DSA Practical PDF
DSA Practical PDF
DSA Practical PDF
#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
#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
#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:
________________________________________________________________
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
#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
#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:
10
Practical No. 6
#include<stdio.h>
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);
return 0;
}
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
}
}
11
i=i1; //beginning of the first list
j=i2; //beginning of the second list
k=0;
________________________________________________________________
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
#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
return 0;
13
}
_________________________________________________________
Output:
_________________________________________________________
14
Practical No.8
#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
#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;
} }}
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
#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");
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
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