DSA Practicals Nayan
DSA Practicals Nayan
VISHWAVIDYALAYA
Department of C.S.E
Answer.
include<stdio.h>
/* Function prototype */
float average(float a[100], int n); int main()
{ float a[100], res; int i, n;
printf("Enter n:\n");
scanf("%d", &n);
/* Reading array */
for(i=0;i< n;i++) {
printf("a[%d]=",i);
scanf("%f", &a[i]);
} /* Function Call */
res = average(a,n);
printf("Average = %f", res);
return 0; }
/* Function definition */
float average(float a[10], int n)
{
int i; float sum=0.0; for(i=0;i< n;i++)
{ sum = sum + a[i];
} return(sum/n); }
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear = -1;
} while(1); return 0; }
Q.5. Write a menu driven program to implement the push, pop and
display option of the stack with the help of dynamic memory
allocation.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct list
{
int data;
struct list *link;
}*top=NULL;
void push(int m)
{
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
tmp->link=top;
top=tmp;
}
void pop()
{
struct list *tmp;
if(top==NULL)
printf("\n\nSTACK IS EMPTY");
else
{
tmp=top;
printf("\n\nDELETED IS ELEMENT %d",tmp->data);
top=top->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(top==NULL)
printf("\nSTACK IS EMPTY");
else
{
q=top;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
do
{
printf("MENU");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\nENTER UR CHOICE: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nENTER THE NUMBER: ");
scanf("%d",&n);
push(n);
break;
case 2:
pop();
break;
case 3:
printf("\nSTACK ELEMENTS ARE \n");
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
getch();
}
Q 6. Write a menu driven program to implementing the various
operations on a linear queue with the help of static memory
allocation.
#include<stdio.h>
#include<stdlib.h>
#define MAX 3
int a[MAX];
int front=-1;
int rear=-1;
void insert();
void del();
void disp();
void main()
{
int ch;
do
{
printf("\nMenu \n");
printf("\n1.INSERT");
printf("\n2.del");
printf("\n3.disp");
printf("\n4.exit");
printf("\nEnter ur choice");
scanf(“%d”,&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
gets();
}
void insert()
{
int n;
if(rear==MAX-1)
{
printf("Q is Full");
}
else
{
if(front==-1)
{
front=0;
}
else
{
rear++;
printf("\nEnter the Element");
scanf("%d",&n);
a[rear]=n;
}
}
}
void del()
{
if(front==-1)
{
printf("\nQ is Empty");
}
else
{
printf("Deleted Element is %d", a[front]);
front++;
}
}
void disp()
{
int i;
if(front==-1)
{
printf("\nQ is Empty");
}
else
{
for(i=front;i<=rear;i++)
{
printf("%d->",a[i]);
}
#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *link;
}*front=NULL,*rear=NULL;
void create(int m)
{
struct list *tmp;
tmp=(struct list *)malloc(sizeof(struct list));
tmp->data=m;
tmp->link=NULL;
if(front==NULL)
front=tmp;
else
rear->link=tmp;
rear=tmp;
}
void del()
{
struct list *tmp;
if(front==NULL)
printf("\n\nQUEUE IS FULL");
else
{
tmp=front;
printf("\n\nDELETED IS ELEMENT %d" ,tmp->data);
front=front->link;
free(tmp);
}
}
void disp()
{
struct list *q;
if(front==NULL)
printf("\n\nQUEUE IS EMPTY");
else
{
q=front;
while(q!=NULL)
{
printf("%d==>",q->data);
q=q->link;
}
}
}
void main()
{
int i,n,ch;
clrscr();
do
{
printf("\n\nMENU");
printf("\n\n1.INSERT");
printf("\n\n2.DELETE ");
printf("\n\n3.DISPLAT ");
printf("\n\n4.EXIT");
printf("\n\nENTER UR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nENTER THE NUMBER");
scanf("%d",&n);
create(n);
break;
case 2:
del();
break;
case 3:
printf("\n\nQUEUE ELEMENTS ARE \n\n");
disp();
break;
case 4:
exit(0);
}
}
while(ch!=4);
gets();
}
Q 8. Write a menu driven program to implement various operations
on a linear linked list.
#include <stdio.h>
#include <stdlib.h>
// Create a node
struct Node {
int data;
struct Node* next;
};
new_node->next = (*head_ref);
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
}
// Delete a node
void deleteNode(struct Node** head_ref, int key) {
struct Node *temp = *head_ref, *prev;
free(temp);
}
// Search a node
int searchNode(struct Node** head_ref, int key) {
struct Node* current = *head_ref;
if (head_ref == NULL) {
return;
} else {
while (current != NULL) {
// index points to the node next to current
index = current->next;
// Driver program
int main() {
struct Node* head = NULL;
insertAtEnd(&head, 1);
insertAtBeginning(&head, 2);
insertAtBeginning(&head, 3);
insertAtEnd(&head, 4);
insertAfter(head->next, 5);
int item_to_find = 3;
if (searchNode(&head, item_to_find)) {
printf("\n%d is found", item_to_find);
} else {
printf("\n%d is not found", item_to_find);
}
sortLinkedList(&head);
printf("\nSorted List: ");
printList(head);
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*start;
void addlast(int);
void create(int);
void disp();
void addbeg(int);
void main()
{
int ch,n,i,m,a,pos;
start=NULL;
do
{
printf("\n\nMENU\n\n");
printf("\n1.CREATE\n");
printf("\n2.DISPLAY\n");
printf("\n3.ADDBEG\n");
printf("\n4.ADDLAST \n");
printf("\n5.EXIT\n");
printf("\nENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\nHOW MANY NODES U WANT TO CREATE\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nENTER THE DATA");
scanf("%d",&m);
create(m);
}
break;
case 2:
disp();
break;
case 5:
exit(0);
case 3:
printf("\nENTER THE VALUE FOR NODE");
scanf("%d",&a);
addbeg(a);
break;
case 4:
printf("\nENTER THE VALUE FOR NODE\n");
scanf("%d",&m);
addlast(m);
break;
}
}
while(ch!=5);
getch();
}
void create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
if(start==NULL)
{
start=tmp;
}
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
}
void disp()
{
struct node *q;
if(start==NULL)
{
printf("\n\nLIST IS EMPTY");
}
else
{
q=start;
while(q!=NULL)
{
printf("%d->",q->data);
q=q->link;
}
printf("NULL");
}
}
void addbeg(int data)
{
struct node *tmp;
tmp=(struct node *)malloc(sizeof(struct node ));
tmp->data=data;
tmp->link=start;
start=tmp;
}
void addlast(int data)
{
struct node *q,*tmp;
tmp=(struct node *) malloc(sizeof(struct node));
tmp->data=data;
tmp->link=NULL;
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
#include <stdio.h>
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
bubbleSort(data, size);
Q 11. Program for Insertion sort 12. Program for Merge Sort
C program for insertion sort
#include <math.h>
#include <stdio.h>
int i, key, j;
key = arr[i];
j = i - 1;
/* Move elements of arr[0..i-1], that are greater than key, to one position ahead
of their current position */
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
// A utility function to print an array of size n
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver program to test insertion sort */
int main()
{
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}
// Merge sort in C
#include <stdio.h>
// Divide the array into two subarrays, sort them and merge them
void mergeSort(int arr[], int l, int r) {
if (l < r) {
// m is the point where the array is divided into two subarrays
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
// Driver program
int main() {
int arr[] = {6, 5, 12, 10, 9, 1};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, size - 1);
// Heap Sort in C
#include <stdio.h>
// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}
// Driver code
int main() {
int arr[] = {1, 12, 9, 5, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
// Quick sort in C
#include <stdio.h>
// main function
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
printf("Unsorted Array\n");
printArray(data, n);
#include <stdio.h>
#include <stdlib.h>
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
return node;
}
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// If the node has two children
struct node *temp = minValueNode(root->right);
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
printf("Inorder traversal: ");
inorder(root);
Search operations-
Algorithm:
Algorithm:
If root == NULL return NULL; If number == root->data return root->data; If
number < root->data return search(root->left) If number > root->data return
search(root->right)
Insert operations-
Algorithm:
If node == NULL return createNode(data) if (data < node->data) node->left =
insert(node->left, data); else if (data > node->data) node->right = insert(node-
>right, data); return node;
Delete operations-
Algorithm:
1.Starting at the root, find the deepest and rightmost node in binary
treeand node which we want to delete.
2.Replace the deepest rightmost node’s data with the node to be deleted.
3.Then delete the deepest rightmost node
struct node {
int key;
struct node *left, *right;
};
// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);
// Traverse root
printf("%d -> ", root->key);
// Traverse right
inorder(root->right);
}
}
// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);
return node;
}
return current;
}
// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;
else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}
// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);
18. To develop an algorithm for binary search and perform the same.
Answer-
#include <stdio.h>
int iterativeBinarySearch(int array[], int start_index, int end_index, int element)
{ while
(start_index <= end_index)
{ int middle = start_index + (end_index- start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] < element)
start_index = middle + 1;
else
end_index = middle - 1; }
return -1; }
int main(void)
{ int array[] = {1, 4, 7, 9, 16, 56, 70};
int n = 7;
int element = 16;
int found_index = iterativeBinarySearch(array, 0, n-1, element);
if(found_index == -1 )
{ printf("Element not found in the array ");
}
else { printf("Element found at index : %d",found_index);
} return 0; }
#include <stdio.h>
int recursiveBinarySearch(int array[], int start_index, int end_index, int element)
{ if (end_index >= start_index){
int middle = start_index + (end_index - start_index )/2;
if (array[middle] == element)
return middle;
if (array[middle] > element)
return recursiveBinarySearch(array, start_index, middle-1, element);
return recursiveBinarySearch(array, middle+1, end_index, element);
} return -1; }
int main(void){ int array[] = {1, 4, 7, 9, 16, 56, 70}; int n = 7;
int element = 9;
int found_index = recursiveBinarySearch(array, 0, n-1, element);
if(found_index == -1 ) { printf("Element not found in the array ");
}
else { printf("Element found at index : %d",found_index);
} return 0; }