DSA Practical PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Practical No.

Write a program to implement Binary tree using Linked List.

#include <stdio.h>
#include <malloc.h>
struct node {
struct node * left;
char data;
struct node * right;
};
struct node *constructTree( int );
void inorder(struct node *);
char array[ ] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', '\0', '\0', 'H' };
int leftcount[ ] = { 1, 3, 5, -1, 9, -1, -1, -1, -1, -1 };
int rightcount[ ] = { 2, 4, 6, -1, -1, -1, -1, -1, -1, -1 };
void main() {
struct node *root;
root = constructTree( 0 );
printf("In-order Traversal: \n");
inorder(root);
}
struct node * constructTree( int index ) {
struct node *temp = NULL;
if (index != -1) {
temp = (struct node *)malloc( sizeof ( struct node ) );
temp->left = constructTree( leftcount[index] );
temp->data = array[index];
temp->right = constructTree( rightcount[index] );
}
return temp;
}
void inorder( struct node *root ) {
if (root != NULL) {
inorder(root->left);
printf("%c\t", root->data);
inorder(root->right);
}
}

_____________________________________________________________
Output:

In-order Traversal:
D B H E A F C G

1
Practical No. 2

Perform Search and Delete operation in Binary Search Tree.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *left, *right;
};
struct node *newNode(int item)
{
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
struct node* insert(struct node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
struct node * minValueNode(struct node* node)
{
struct node* current = node;
while (current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, int key)
{

2
if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);

else
{
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;
}
struct node* temp = minValueNode(root->right);
root->key = temp->key;
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int main()
{
struct node *root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
int val;
printf("Inorder traversal of the given tree \n");
inorder(root);
printf("\nEnter the value to be deleted:");
scanf("%d", &val);
root = deleteNode(root, val);
printf("Inorder traversal of the modified tree \n");
inorder(root);
return 0;
}

3
Output:
__________________________________________________________
20 30 40 50 60 70 80
Enter the value to be deleted:30
Inorder traversal of the modified tree
20 40 50 60 70 80

4
Practical No. 3

Write a program for Depth First Search using Recursion.

#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
struct node *left;
struct node *right;
};
void generate(struct node **, int);
void DFS(struct node *);
void delete(struct node **);
int main()
{
struct node *head = NULL;
int choice = 0, num, flag = 0, key;
do
{
printf("\nEnter your choice:\n1. Insert\n2. Perform DFS Traversal\n3.
Exit\nChoice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter element to insert: ");
scanf("%d", &num);
generate(&head, num);
break;
case 2:
DFS(head);
break;
case 3:
delete(&head);
printf("Memory Cleared\nPROGRAM TERMINATED\n");
break;
default:
printf("Not a valid input, try again\n");
}
} while (choice != 3);
return 0;
}

5
void generate(struct node **head, int num)
{
struct node *temp = *head, *prev = *head;
if (*head == NULL)
{
*head = (struct node *)malloc(sizeof(struct node));
(*head)->a = num;
(*head)->left = (*head)->right = NULL;
}
else
{
while (temp != NULL)
{
if (num > temp->a)
{
prev = temp;
temp = temp->right;
}
else
{
prev = temp;
temp = temp->left;
}
}
temp = (struct node *)malloc(sizeof(struct node));
temp->a = num;
if (num >= prev->a)
{
prev->right = temp;
}
else
{
prev->left = temp;
}
}
}
void DFS(struct node *head)
{
if (head)
{
if (head->left)
{
DFS(head->left);
}
if (head->right)
{
DFS(head->right);

6
}
printf("%d ", head->a);
}
}
void delete(struct node **head)
{
if (*head != NULL)
{
if ((*head)->left)
{
delete(&(*head)->left);
}
if ((*head)->right)
{
delete(&(*head)->right);
}
free(*head);
}
}

________________________________________________________________
Output:
________________________________________________________________

Enter your choice:


1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 30

Enter your choice:


1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 31

Enter your choice:


1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 1
Enter element to insert: 1

7
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 2
1 31 30
Enter your choice:
1. Insert
2. Perform DFS Traversal
3. Exit
Choice: 3

8
Practical No.4

Write a program to count the number of nodes in Linked List.

#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;};
void push(struct Node** head_ref, int new_data)
{
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int getCount(struct Node* head)
{
int count = 0;
struct Node* current = head;
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
int main()
{
struct Node* head = NULL;
push(&head, 1);
push(&head, 3);
push(&head, 1);
push(&head, 2);
push(&head, 1);
printf("count of nodes is %d", getCount(head));
return 0;
}
________________________________________________________________

Output :

count of nodes is 5

9
Practical No. 5

Write a program to implement Binary Search in C.

#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d found at location %d.\n", search, middle+1);
break;}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);
return 0;
}

________________________________________________________________

Output:

Enter number of elements


10
Enter 10 integers
3 5 6 1 9 8 7 10 11 20
Enter value to find
10
10 found at location 8.

10
Practical No. 6

Write a program to implement Merge sort in C.

#include<stdio.h>

void mergesort(int a[],int i,int j);


void merge(int a[],int i1,int j1,int i2,int j2);

int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");

for(i=0;i<n;i++)
scanf("%d",&a[i]);

mergesort(a,0,n-1);

printf("\nSorted array is :");


for(i=0;i<n;i++)
printf("%d ",a[i]);

return 0;
}

void mergesort(int a[],int i,int j)


{
int mid;

if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}

void merge(int a[],int i1,int j1,int i2,int j2)


{
int temp[50]; //array used for merging
int i,j,k;

11
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;

while(i<=j1 && j<=j2) //while elements in both lists


{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}

while(i<=j1) //copy remaining elements of the first list


temp[k++]=a[i++];

while(j<=j2) //copy remaining elements of the second list


temp[k++]=a[j++];

//Transfer elements from temp[] back to a[]


for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}

________________________________________________________________

Output:
________________________________________________________________

Enter no of elements: 5
Enter array elements: 40 82 5 21 39

Sorted array is :5 21 39 40 82

12
Practical No.7

Write a program to implement Selection sort – [in Ascending order and


descending order].

#include <stdio.h>

int main()
{
int array[100], n, c, d, position, swap;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

for ( c = 0 ; c < ( n - 1 ) ; c++ )


{
position = c;

for ( d = c + 1 ; d < n ; d++ )


{
if ( array[position] > array[d] )
position = d;
}
if ( position != c )
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}

printf("Sorted list in ascending order:\n");

for ( c = 0 ; c < n ; c++ )


printf(" %d ", array[c]);
printf("\nSorted list in descending order:\n");

for ( c = n-1 ; c >-1 ; c-- )


printf(" %d ", array[c]);

return 0;

13
}

_________________________________________________________
Output:
_________________________________________________________

Enter number of elements


5
Enter 5 integers
93 12 5 40 23
Sorted list in ascending order:
5 12 23 40 93
Sorted list in descending order:
93 40 23 12 5

14
Practical No.8

Write a program to perform various operations in a linear array: Traversing,


Insertion and Deletion.

#include<stdio.h>
#include<conio.h>
int main()
{ int ch;
printf("Enter 1 for insertion into program\n");
printf("Enter 2 for deletion into program\n");
printf("Enter 3 for traversing\n");
printf("Enter your choice :\n");
scanf("%d",&ch);
switch(ch)
{
case 1 :
{
int i,pos,n,k,a[50];
printf("Enter how many data elements you want to enter :\n" );
scanf("%d",&n);
printf("Enter your desired elements :\n" );
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("You entered : ");
for(i=0;i<=n-1;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter the postion where you want to insert the new element :\n");
scanf("%d",&pos);
printf("Enter the data element you want to insert :\n");
scanf("%d",&k);
for(i=n-1;i>=pos-1;i--)
{
a[i+1]=a[i];
}
a[pos-1]=k;
printf("Your new sequensce of elements is :\n");
for(i=0;i<=n;i++)
{
printf("%d ",a[i]);
}

15
getch();
break;
}
case 2:
{
int i,pos,j,n,a[50];
printf("Enter how many data elements you want to enter :\n" );
scanf("%d",&n);
printf("Enter your desired elements :\n" );
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("You entered : ");
for(i=0;i<=n-1;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter the postion of element you want to delete :\n");
scanf("%d",&pos);
for(i=pos-1;i<=n-1;i++)
{
a[i]=a[i+1];
}
printf("Your new sequence of elements is :\n");
for(i=0;i<=n-2;i++)
{
printf("%d ",a[i]);
}
getch();
break;
}

case 3:
{
int i,n,a[50];
printf("Enter the no. elements you want to enter :\n");
scanf("%d",&n);
printf("Enter your data elements :\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
printf("You entered : ");
for(i=0;i<=n-1;i++)

16
{
printf("%d ",a[i]);
}
getch();
break;
}
}
}

________________________________
Output
________________________________
Enter 1 for insertion into program
Enter 2 for deletion into program
Enter 3 for traversing
Enter your choice :
2
Enter how many data elements you want to enter :
4
Enter your desired elements :
12
52
34
855
You entered : 12 52 34 855
Enter the postion of element you want to delete :
3
Your new sequence of elements is :
12 52 855

17
Practical No.9

Write a program to implement Bubble Sort.

#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
} }}

printf("Sorted list in ascending order: ");

for ( c = 0 ; c < n ; c++ )


printf("%d ", array[c]);

return 0;
}

________________________________________________
Output:
________________________________________________
Enter number of elements
5
Enter 5 integers
20 40 12 7 39
Sorted list in ascending order: 7 12 20 39 40

18
Practical No. 10

Write a program to insert an element as first node in a circular list.

#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * nextptr;
}*stnode;
void ClListcreation(int n);
void ClLinsertNodeAtBeginning(int num);
void displayClList(int a);
int main()
{
int n,num1,a;
stnode = NULL;
printf("\n\n Circular Linked List : Insert a node at the beginning of a circular linked
list :\n");
printf("--------------------------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
ClListcreation(n);
a=1;
displayClList(a);
printf(" Input data to be inserted at the beginning : ");
scanf("%d", &num1);
ClLinsertNodeAtBeginning(num1);
a=2;
displayClList(a);
return 0;
}
void ClListcreation(int n)
{
int i, num;
struct node *preptr, *newnode;
if(n >= 1)
{
stnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->nextptr = NULL;
preptr = stnode;
for(i=2; i<=n; i++)

19
{
newnode = (struct node *)malloc(sizeof(struct node));
printf(" Input data for node %d : ", i);
scanf("%d", &num);
newnode->num = num;
newnode->nextptr = NULL; // next address of new node set as NULL
preptr->nextptr = newnode; // previous node is linking with new node
preptr = newnode; // previous node is advanced
}
preptr->nextptr = stnode; //last node is linking with first node
}
}
void ClLinsertNodeAtBeginning(int num)
{
struct node *newnode, *curNode;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
newnode = (struct node *)malloc(sizeof(struct node));
newnode->num = num;
newnode->nextptr = stnode;
curNode = stnode;
while(curNode->nextptr != stnode)
{
curNode = curNode->nextptr;
}
curNode->nextptr = newnode;
stnode = newnode;
}
}
void displayClList(int m)
{
struct node *tmp;
int n = 1;
if(stnode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = stnode;
if (m==1)
{
printf("\n Data entered in the list are :\n");

20
}
else
{
printf("\n After insertion the new list are :\n");
}
do {
printf(" Data %d = %d\n", n, tmp->num);
tmp = tmp->nextptr;
n++;
}while(tmp != stnode);
}
}

___________________________________________________________

Output:
____________________________________________________________
Circular Linked List : Insert a node at the beginning of a circular linked list :
--------------------------------------------------------------------------------------
Input the number of nodes : 3
Input data for node 1 : 10
Input data for node 2 : 20
Input data for node 3 : 20

Data entered in the list are :


Data 1 = 10
Data 2 = 20
Data 3 = 20
Input data to be inserted at the beginning : 40

After insertion the new list are :


Data 1 = 40
Data 2 = 10
Data 3 = 20
Data 4 = 20

21
Practical No. 11
Write a program to perform push and pop operations in a stack.

#include<stdio.h>
#include<conio.h>
#define max 5
void main()
{
int stack[max],data;
int top,option,reply;
top = -1;
clrscr();
do
{
printf("\n 1. push");
printf("\n 2. pop");
printf("\n 3. exit");
printf("\nSelect proper option : ");
scanf("%d",&option);
switch(option)
{
case 1 :
printf("\n Enter a value : ");
scanf("%d",&data);
reply = push(stack,&top,&data);
if( reply == -1 )
printf("\nStack is full");
else
printf("\n Pushed value");
break;
case 2 :
reply = pop ( stack,&top,&data);
if( reply == - 1)
printf("\nStack is empty");
else
printf("\n Popped value is %d",data);
break;
case 3 : exit(0);
}
}while(1);
}
int push( int stack[max],int *top, int *data)
{
if( *top == max -1 )
return(-1);
else

22
{
*top = *top + 1;
stack[*top] = *data;
return(1);
}
}
int pop( int stack[max], int *top, int *data)
{
if( *top == -1 )
return(-1);
else
{
*data = stack[*top];
*top = *top - 1;
return(1);
}
}

_____________________________________________________________
Output:

1. push
2. pop
3. exit
Select proper option : 1

Enter a value : 12

Pushed value
1. push
2. pop
3. exit
Select proper option : 1

Enter a value : 56

Pushed value
1. push
2. pop
3. exit
Select proper option : 2

Popped value is 56
1. push
2. pop
3. exit
Select proper option : 3

23

You might also like