0% found this document useful (0 votes)
5 views

Data Structure Lab Manual R20 CSE (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Data Structure Lab Manual R20 CSE (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

Data Structures LAB Manual R20 2021

List of Experiments:
Exercise -1 (Searching)
a)Write C program that use both recursive and non-recursive functions to perform Linear search
for a Key value in a given list.
b) Write C program that use both recursive and non-recursive functions to perform Binary search
for a Key value in a given list.

Exercise -2 (Sorting-I)
a) Write C program that implement Bubble sort, to sort a given list of integers in ascending order
b) Write C program that implement Quick sort, to sort a given list of integers in ascending order
c) Write C program that implement Insertion sort, to sort a given list of integers in ascending
order

Exercise -3(Sorting-II)
a) Write C program that implement radix sort, to sort a given list of integers in ascending order
b) Write C program that implement merge sort, to sort a given list of integers in ascending order

Exercise -4(Singly Linked List)


a) Write a C program that uses functions to create a singly linked list
b) Write a C program that uses functions to perform insertion operation on a singly linked list
c) Write a C program that uses functions to perform deletion operation on a singly linked list
d) Write a C program to reverse elements of a single linked list.

Exercise -5(Queue)
a) Write C program that implement Queue (its operations) using arrays.
b) Write C program that implement Queue (its operations) using linked lists

Exercise -6(Stack)
a) Write C program that implement stack (its operations) using arrays
b) Write C program that implement stack (its operations) using Linked list
c) Write a C program that uses Stack operations to evaluate postfix expression

Exercise -7(Binary Tree)


d) Write a recursive C program for traversing a binary tree in preorder, inorder and postorder.

Exercise -8(Binary Search Tree)


a) Write a C program to Create a BST
b) Write a C program to insert a node into a BST.
c) Write a C program to delete a node from a BST.

Sateesh Kumar YNV Page 1


Data Structures LAB Manual R20 2021

Exercise -1 (Searching)
a)Write C program that use both recursive and non-recursive functions to perform Linear
search for a Key value in a given list.
Program:

#include <stdio.h>
#define MAX_LEN 10

void l_search_recursive(int l[],int num,int ele);


void l_search_nonrecursive(int l[],int num,int ele);
void l_search(int l[],int num,int ele);
void read_list(int l[],int num);
void print_list(int l[],int num);

int main() {
int l[MAX_LEN], num, ele;
int ch;
printf("======================================================");
printf("\n\t\t\tMENU");
printf("\n=====================================================");
printf("\n[1] Linary Search using Recursion method");
printf("\n[2] Linary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0) {


printf("Enter the number of elements :");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nElement you want to search:\n\n");
scanf("%d",&ele);

switch(ch) {
case 1:
printf("\n**Recursion method**\n");
l_search_recursive(l,num,ele);
break;

case 2:
printf("\n**Non-Recursion method**\n");
l_search_nonrecursive(l,num,ele);

Sateesh Kumar YNV Page 2


Data Structures LAB Manual R20 2021

break;
}
}
return 0;
}
/*end main*/

/* Non-Recursive method*/
void l_search_nonrecursive(int l[],int num,int ele) {
int j, f=0;
for(j=0; j<num; j++) {
if( l[j] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,j);
f=1;
break;
}
if(f==0)
printf("\nThe element is %d is not present in the list\n",ele);
}

/* Recursive method*/
void l_search_recursive(int l[],int num,int ele) {
int f = 0;
if( l[num] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,num);
f=1;
} else {
if((num==0) && (f==0)) {
printf("The element %d is not found.",ele);
} else {
l_search_recursive(l,num-1,ele);
}
}
}

void read_list(int l[],int num) {


int j;
printf("\nEnter the elements:\n");
for(j=0; j<num; j++)
scanf("%d",&l[j]);
}

Sateesh Kumar YNV Page 3


Data Structures LAB Manual R20 2021

void print_list(int l[],int num) {


int j;
for(j=0; j<num; j++)
printf("%d\t",l[j]);
}

Output:
======================================================
MENU
=====================================================
[1] Linary Search using Recursion method
[2] Linary Search using Non-Recursion method

Enter your Choice:1


Enter the number of elements :8

Enter the elements:


13
15
19
20
24
26
29
68

Elements present in the list are:


13 15 19 20 24 26 29 68

Element you want to search:


26

**Recursion method**

The element 26 is present at position 5 in list

--------------------------------
Process exited after 20.71 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 4


Data Structures LAB Manual R20 2021

Output 2:

======================================================
MENU
=====================================================
[1] Linary Search using Recursion method
[2] Linary Search using Non-Recursion method

Enter your Choice:2


Enter the number of elements :8

Enter the elements:


11
22
33
45
56
78
98
167

Elements present in the list are:


11 22 33 45 56 78 98 167

Element you want to search:


98

**Non-Recursion method**

The element 98 is present at position 6 in list


--------------------------------
Process exited after 18.58 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 5


Data Structures LAB Manual R20 2021

Exercise -1 (Searching)
b) Write C program that use both recursive and non-recursive functions to perform
Binary search for a Key value in a given list.

Program:
#include <stdio.h>
#define MAX_LEN 10

/* Non-Recursive function*/
void b_search_nonrecursive(int l[],int num,int ele) {
int l1,i,j, flag = 0;
l1 = 0;
i = num-1;
while(l1 <= i) {
j = (l1+i)/2;
if( l[j] == ele) {
printf("\nThe element %d is present at position %d in list\n",ele,j);
flag =1;
break;
} else if(l[j] < ele)
l1 = j+1;
else
i = j-1;
}
if( flag == 0)
printf("\nThe element %d is not present in the list\n",ele);
}

/* Recursive function*/
int b_search_recursive(int l[],int arrayStart,int arrayEnd,int a) {
int m,pos;
if (arrayStart<=arrayEnd) {
m=(arrayStart+arrayEnd)/2;
if (l[m]==a)
return m;
else if (a<l[m])
return b_search_recursive(l,arrayStart,m-1,a);
else
return b_search_recursive(l,m+1,arrayEnd,a);
}
return -1;
}

Sateesh Kumar YNV Page 6


Data Structures LAB Manual R20 2021

void read_list(int l[],int n) {


int i;
printf("\nEnter the elements:\n");
for(i=0; i<n; i++)
scanf("%d",&l[i]);
}

void print_list(int l[],int n) {


int i;
for(i=0; i<n; i++)
printf("%d\t",l[i]);
}

/*main function*/
int main() {
int l[MAX_LEN], num, ele,f,l1,a;
int ch,pos;

//clrscr();

printf("=====================================================
=");
printf("\n\t\t\tMENU");
printf("\n====================================================
=");
printf("\n[1] Binary Search using Recursion method");
printf("\n[2] Binary Search using Non-Recursion method");
printf("\n\nEnter your Choice:");
scanf("%d",&ch);

if(ch<=2 & ch>0) {


printf("\nEnter the number of elements : ");
scanf("%d",&num);
read_list(l,num);
printf("\nElements present in the list are:\n\n");
print_list(l,num);
printf("\n\nEnter the element you want to search:\n\n");
scanf("%d",&ele);

switch(ch) {
case 1:
printf("\nRecursive method:\n");

Sateesh Kumar YNV Page 7


Data Structures LAB Manual R20 2021

pos=b_search_recursive(l,0,num,ele);
if(pos==-1) {
printf("Element is not found");
} else {
printf("Element is found at %d position",pos);
}
//getch();
break;

case 2:
printf("\nNon-Recursive method:\n");
b_search_nonrecursive(l,num,ele);
//getch();
break;
}
}
//getch();
}
Output:

======================================================
MENU
=====================================================

[1] Binary Search using Recursion method


[2] Binary Search using Non-Recursion method
Enter your Choice:1
Enter the number of elements : 8
Enter the elements: 12 23 45 56 78 89 123 456
Elements present in the list are:
12 23 45 56 78 89 123 456
Enter the element you want to search:
123
Recursive method:
Element is found at 6 position
--------------------------------
Process exited after 18.19 seconds with return value

Output 2:
======================================================
MENU
=====================================================
[1] Binary Search using Recursion method

Sateesh Kumar YNV Page 8


Data Structures LAB Manual R20 2021

[2] Binary Search using Non-Recursion method

Enter your Choice:2

Enter the number of elements : 8

Enter the elements:


13
24
35
49
57
68
97
102
Elements present in the list are:
13 24 35 49 57 68 97 102

Enter the element you want to search:


49

Non-Recursive method:
The element 49 is present at position 3 in list

--------------------------------
Process exited after 26.74 seconds with return value

Sateesh Kumar YNV Page 9


Data Structures LAB Manual R20 2021

Exercise -2 (Sorting-I)
a) Write C program that implement Bubble sort, to sort a given list of integers in
ascending order
Program:

/* C program to implement bubble sort */


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

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

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


printf("%d\n", array[i]);
return 0;
}
Output:
Enter number of elements
8
Enter 8 integers
65
32
18
95

Sateesh Kumar YNV Page 10


Data Structures LAB Manual R20 2021

4
978
62
15
Sorted list in ascending order:
4
15
18
32
62
65
95
978

--------------------------------
Process exited after 8.559 seconds with return value

Sateesh Kumar YNV Page 11


Data Structures LAB Manual R20 2021

Exercise -2 (Sorting-I)
b) Write C program that implement Quick sort, to sort a given list of integers in ascending
order
Program:
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int n) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d ",b[i]);
}
printf("\n");
}
int partition(int a[],int beg,int end) {
int pivot;
pivot = a[beg];
int i = beg;
int j = end;
while(i<j) {
while(a[i]<=pivot) {
i++;
}
while(a[j]>pivot) {
j--;
}
if(i<j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[beg] = a[j];
a[j] = pivot;
return j;

}
void quicksort(int a[],int begg,int endd) {
if(begg<endd) {
int loc = partition(a,begg,endd);
quicksort(a,begg,loc-1);
quicksort(a,loc+1,endd);
}
}

Sateesh Kumar YNV Page 12


Data Structures LAB Manual R20 2021

int main() {
int a[MAX_SIZE];
int n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
int beg=0;
int end= n-1;
quicksort(a,beg,end);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
62
-959
62
31
48
87
625
-16
Elements Before Sorting:
62 -959 62 31 48 87 625 -16
Ascending order Elements:
-959 -16 31 48 62 62 87 625

--------------------------------
Process exited after 13.03 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 13


Data Structures LAB Manual R20 2021

Exercise -2 (Sorting-I)
c) Write C program that implement Insertion sort, to sort a given list of integers in
ascending order
Program:
#include<stdio.h>
#include<stdlib.h>
#define MAX_SIZE 1000
void display(int b[],int n) {
printf("\n");
for(int i=0; i<n; i++) {
printf("%d ",b[i]);
}
printf("\n");
}
void insertion(int a[],int n) {
for(int i=1; i<n; i++) {
int temp = a[i];
int j=i-1;
while(temp<=a[j]&& j>=0) {
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
}
int main() {
int a[MAX_SIZE];
int n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
//Logic for insertion sort
insertion(a,n);
//Display of array elements
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}

Sateesh Kumar YNV Page 14


Data Structures LAB Manual R20 2021

Output:
Enter the size of Array:10
Enter 10 integers
65
32
18
94
65
-12
65
48
-84
32

Elements Before Sorting:

65 32 18 94 65 -12 65 48 -84 32

Ascending order Elements:

-84 -12 18 32 32 48 65 65 65 94

--------------------------------
Process exited after 13.44 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 15


Data Structures LAB Manual R20 2021

Exercise -3(Sorting-II)
a) Write C program that implement radix sort, to sort a given list of integers in ascending
order
Program:
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int size) {
printf("\n");
for(int i=0; i<size; i++) {
printf("%d\t",b[i]);
}
printf("\n");
}
int bigno(int d[],int size) {
int large = d[0];
for(int i=1; i<size; i++) {
if(large<d[i]) {
large = d[i];
}
}
return large;
}
void countsort(int d[],int size1,int pos1) {
int count[10];
int aux[8];
for(int i=0; i<10; i++) {
count[i] = 0;
}
for(int i=0; i<size1; i++) {
count[(d[i]/pos1)%10]++;
}
for(int i=1; i<10; i++) {
count[i] = count[i]+count[i-1];
}
for(int i=size1-1; i>=0; i--) {
aux[--count[(d[i]/pos1)%10]]=d[i];
}
for(int i=0; i<size1; i++) {
d[i]=aux[i];
}
}
void radixsort(int c[],int size) {
int digit= bigno(c,size);

Sateesh Kumar YNV Page 16


Data Structures LAB Manual R20 2021

//printf("%d",digit);
for(int pos =1; digit/pos>0; pos*=10) {
countsort(c,size,pos);
}
}
int main() {
int a[MAX_SIZE],n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
radixsort(a,n);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
12
3
48
65
952
6478
23
48
Elements Before Sorting:
12 3 48 65 952 6478 23 48
Ascending order Elements:
3 12 23 48 48 65 952 6478
--------------------------------
Process exited after 13.43 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 17


Data Structures LAB Manual R20 2021

Exercise -3(Sorting-II)
b) Write C program that implement merge sort, to sort a given list of integers in ascending
order
Program:
// merge sort
#include<stdio.h>
#define MAX_SIZE 1000
void display(int b[],int no) {
int i=0;
printf("\n");
while(i<no) {
printf("%d ",*(b+i));
i++;
}
printf("\n");
}
//Merging elements after dividing individual elements
void merge(int c[],int begi,int midd,int endi) {
int arr[10];
int i = begi;
int j = midd+1;
int k = begi;
while(i<=midd&&j<=endi){
if(c[i]<c[j]){
arr[k] = c[i];
i++;
}else{
arr[k] = c[j];
j++;
}
k++;
}
if(i>midd){
while(j<=endi){
arr[k] = c[j];
j++;
k++;
}
}else{
while(i<=midd){
arr[k] = c[i];
i++;
k++;

Sateesh Kumar YNV Page 18


Data Structures LAB Manual R20 2021

}
}
for(int i=begi;i<k;i++)
c[i] = arr[i];
}
//Merge sort algorithm
void mergesort(int b[],int beg,int end) {
if(beg<end) {
int mid = (beg+end)/2;
mergesort(b,beg,mid);
mergesort(b,mid+1,end);
merge(b,beg,mid,end);
}
}
int main() {
int a[MAX_SIZE],n;
printf("Enter the size of Array:");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for (int i= 0; i < n; i++) {
scanf("%d", &a[i]);
}
printf("\nElements Before Sorting:\n");
display(a,n);
int begg=0;
int endd=n-1;
mergesort(a,begg,endd);
printf("\nAscending order Elements: \n");
display(a,n);
return 0;
}
Output:
Enter the size of Array:8
Enter 8 integers
95
62
31
84
75
-65
98
-23

Sateesh Kumar YNV Page 19


Data Structures LAB Manual R20 2021

Elements Before Sorting:

95 62 31 84 75 -65 98 -23

Ascending order Elements:

-65 -23 31 62 75 84 95 98

--------------------------------
Process exited after 14.01 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 20


Data Structures LAB Manual R20 2021

Exercise -4(Singly Linked List)


a) Write a C program that uses functions to create a singly linked list
b) Write a C program that uses functions to perform insertion operation on a singly linked
list
c) Write a C program that uses functions to perform deletion operation on a singly linked
list
Program:
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node {
int info;
struct node *next;
};
struct node *start=NULL;
int main() {
int choice;
while(1) {

printf("\n------------MENU--------------------\n");
printf("\n 1.Create");
printf("\n 2.Display ");
printf("\n 3.Insert at the beginning");
printf("\n 4.Insert at the end ");
printf("\n 5.Insert at specified position");
printf("\n 6.Delete from beginning");
printf("\n 7.Delete from the end");
printf("\n 8.Delete from specified position");
printf("\n 9.Exit");
printf("\n--------------------------------------\n");
printf("Enter your choice:");
scanf("%d",&choice);
switch(choice) {
case 1:
create();
break;

Sateesh Kumar YNV Page 21


Data Structures LAB Manual R20 2021

case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;

case 9:
exit(0);
break;

default:
printf("\n Wrong Choice:\n");
break;
}
}
return 0;
}
void create() {
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
exit(0);
}
printf("\nEnter the data value for the node:\t");
scanf("%d",&temp->info);
temp->next=NULL;

Sateesh Kumar YNV Page 22


Data Structures LAB Manual R20 2021

if(start==NULL) {
start=temp;
} else {
ptr=start;
while(ptr->next!=NULL) {
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display() {
struct node *ptr;
if(start==NULL) {
printf("\nList is empty:\n");
return;
} else {
ptr=start;
printf("\nThe List elements are:\n");
while(ptr!=NULL) {
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_begin() {
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL) {
start=temp;
} else {
temp->next=start;
start=temp;
}
}
void insert_end() {
struct node *temp,*ptr;

Sateesh Kumar YNV Page 23


Data Structures LAB Manual R20 2021

temp=(struct node *)malloc(sizeof(struct node));


if(temp==NULL) {
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the data value for the node:\t" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL) {
start=temp;
} else {
ptr=start;
while(ptr->next !=NULL) {
ptr=ptr->next ;
}
ptr->next =temp;
}
}
void insert_pos() {
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL) {
printf("\nOut of Memory Space:\n");
return;
}
printf("\nEnter the position for the new node to be inserted:\t");
scanf("%d",&pos);
printf("\nEnter the data value of the node:\t");
scanf("%d",&temp->info) ;

temp->next=NULL;
if(pos==0) {
temp->next=start;
start=temp;
} else {
for(i=0,ptr=start; i<pos-1; i++) {
ptr=ptr->next;
if(ptr==NULL) {
printf("\nPosition not found:[Handle with care]\n");
return;
}
}

Sateesh Kumar YNV Page 24


Data Structures LAB Manual R20 2021

temp->next =ptr->next ;
ptr->next=temp;
}
}
void delete_begin() {
struct node *ptr;
if(ptr==NULL) {
printf("\nList is Empty:\n");
return;
} else {
ptr=start;
start=start->next ;
printf("\nThe deleted element is :%d\t",ptr->info);
free(ptr);
}
}
void delete_end() {
struct node *temp,*ptr;
if(start==NULL) {
printf("\nList is Empty:");
exit(0);
} else if(start->next ==NULL) {
ptr=start;
start=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
} else {
ptr=start;
while(ptr->next!=NULL) {
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\nThe deleted element is:%d\t",ptr->info);
free(ptr);
}
}
void delete_pos() {
int i,pos;
struct node *temp,*ptr;
if(start==NULL) {
printf("\nThe List is Empty:\n");
exit(0);

Sateesh Kumar YNV Page 25


Data Structures LAB Manual R20 2021

} else {
printf("\nEnter the position of the node to be deleted:\t");
scanf("%d",&pos);
if(pos==0) {
ptr=start;
start=start->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
} else {
ptr=start;
for(i=0; i<pos; i++) {
temp=ptr;
ptr=ptr->next ;
if(ptr==NULL) {
printf("\nPosition not Found:\n");
return;
}
}
temp->next =ptr->next ;
printf("\nThe deleted element is:%d\t",ptr->info );
free(ptr);
}
}
}

Output:

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1

Sateesh Kumar YNV Page 26


Data Structures LAB Manual R20 2021

Enter the data value for the node: 23

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1

Enter the data value for the node: 45

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1

Enter the data value for the node: 65

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end

Sateesh Kumar YNV Page 27


Data Structures LAB Manual R20 2021

8.Delete from specified position


9.Exit
--------------------------------------
Enter your choice:187

Wrong Choice:

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1

Enter the data value for the node: 98

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


23 45 65 98
------------MENU--------------------

1.Create
2.Display

Sateesh Kumar YNV Page 28


Data Structures LAB Manual R20 2021

3.Insert at the beginning


4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:4

Enter the data value for the node: 100

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


23 45 65 98 100
------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:3

Enter the data value for the node: 69

Sateesh Kumar YNV Page 29


Data Structures LAB Manual R20 2021

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


69 23 45 65 98 100
------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:8

Enter the position of the node to be deleted: 98

Position not Found:

------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning

Sateesh Kumar YNV Page 30


Data Structures LAB Manual R20 2021

7.Delete from the end


8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


69 23 45 65 98 100
------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:7

The deleted element is:100


------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


69 23 45 65 98
------------MENU--------------------

1.Create
2.Display

Sateesh Kumar YNV Page 31


Data Structures LAB Manual R20 2021

3.Insert at the beginning


4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:8
Enter the position of the node to be deleted: 4
The deleted element is:98
------------MENU--------------------
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2

The List elements are:


69 23 45 65
------------MENU--------------------

1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:9

--------------------------------
Process exited after 88.95 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 32


Data Structures LAB Manual R20 2021

Exercise -4(Singly Linked List)


d) Write a C program to reverse elements of a single linked list.
Program:

#include<stdio.h>
#include<stdlib.h>
void createList(int n);
void reverse_list();
void displayList();

//Creating node with data and a pointer


struct Node {
int data;
struct Node *next;
}*head;

// Function to reverse the linked list


void reverse_list() {
// Initialize current, previous and next pointers
struct Node * current = head;
struct Node* prev = NULL, *next = NULL;
while (current != NULL) {
// Store next
next = current->next;
// Reverse current node's pointer
current->next = prev;
// Move pointers one position ahead
prev = current;
current = next;
}
head = prev;
}

void createList(int n) {
struct Node *newNode, *temp;
int data, i;
head = (struct Node *)malloc(sizeof(struct Node));
// When the list is empty
if(head == NULL) {
printf("Unable to allocate memory.");
} else {
printf("\nEnter the data of node 1: ");
scanf("%d", &data);

Sateesh Kumar YNV Page 33


Data Structures LAB Manual R20 2021

head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++) {
newNode = (struct Node *)malloc(sizeof(struct Node));
if(newNode == NULL) {
printf("Unable to allocate memory.");
break;
} else {
printf("\nEnter the data of node %d: ", i);
scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
}
}
void displayList() {
struct Node *temp;
if(head == NULL) {
printf("List is empty.");
} else {
temp = head;
//Print the list
while(temp != NULL) {
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
}

int main() {
int n;
printf("\nEnter the total number of nodes: ");
scanf("%d", &n);
createList(n);
printf("\nThe List is \n");
displayList();
printf("\nThe Reversed Linked List is\n");

Sateesh Kumar YNV Page 34


Data Structures LAB Manual R20 2021

reverse_list();
displayList();
return 0;
}
Output:

Enter the total number of nodes: 6

Enter the data of node 1: 32

Enter the data of node 2: 65

Enter the data of node 3: 48

Enter the data of node 4: 98

Enter the data of node 5: 65

Enter the data of node 6: 45

The List is
32 65 48 98 65 45

The Reversed Linked List is


45 65 98 48 65 32

--------------------------------
Process exited after 10.66 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 35


Data Structures LAB Manual R20 2021

Exercise -5(Queue)
a) Write C program that implement Queue (its operations) using arrays.
Program:
/*
* C Program to Implement a Queue using an Array
*/
#include <stdio.h>

#define MAX 50

void insert();
void delete();
void display();
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main() {
int choice,del;
while (1) {
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("Wrong choice \n");
} /* End of switch */
} /* End of while */
} /* End of main() */

Sateesh Kumar YNV Page 36


Data Structures LAB Manual R20 2021

void insert() {
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow \n");
else {
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /* End of insert() */

void delete() {
if (front == - 1 || front > rear) {
printf("Queue Underflow \n");
return;
} else {
printf("Element deleted from queue is : %d\n", queue_array[front]);
front = front + 1;
}
} /* End of delete() */

void display() {
int i;
if (front == - 1)
printf("Queue is empty \n");
else {
printf("Queue is : \n");
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
} /* End of display() */

Output:
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit

Sateesh Kumar YNV Page 37


Data Structures LAB Manual R20 2021

Enter your choice : 1


Inset the element in queue : 23
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 59
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
23 59
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 1
Inset the element in queue : 95
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 3
Queue is :
23 59 95
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 23
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Element deleted from queue is : 59
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue

Sateesh Kumar YNV Page 38


Data Structures LAB Manual R20 2021

4.Quit
Enter your choice : 2
Element deleted from queue is : 95
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Queue Underflow
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 2
Queue Underflow
1.Insert element to queue
2.Delete element from queue
3.Display all elements of queue
4.Quit
Enter your choice : 4

--------------------------------
Process exited after 45.16 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 39


Data Structures LAB Manual R20 2021

Exercise -5(Queue)
b) Write C program that implement Queue (its operations) using linked lists
Program:
/*
* C Program to Implement Queue Data Structure using Linked List
*/
#include <stdio.h>
#include <stdlib.h>

struct node {
int info;
struct node *ptr;
}*front,*rear,*temp,*front1;

int frontelement();
void enq(int data);
void deq();
void empty();
void display();
void create();
void queuesize();

int count = 0;

void main() {
int no, ch, e;

printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 3 - Front element");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
printf("\n 7 - Queue size");
create();
while (1) {
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch) {
case 1:
printf("Enter data : ");
scanf("%d", &no);
enq(no);

Sateesh Kumar YNV Page 40


Data Structures LAB Manual R20 2021

break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
printf("Front element : %d", e);
else
printf("\n No front element in Queue as queue is empty");
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create an empty queue */


void create() {
front = rear = NULL;
}

/* Returns queue size */


void queuesize() {
printf("\n Queue size : %d", count);
}

/* Enqueing the queue */


void enq(int data) {
if (rear == NULL) {
rear = (struct node *)malloc(1*sizeof(struct node));

Sateesh Kumar YNV Page 41


Data Structures LAB Manual R20 2021

rear->ptr = NULL;
rear->info = data;
front = rear;
} else {
temp=(struct node *)malloc(1*sizeof(struct node));
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;

rear = temp;
}
count++;
}

/* Displaying the queue elements */


void display() {
front1 = front;

if ((front1 == NULL) && (rear == NULL)) {


printf("Queue is empty");
return;
}
while (front1 != rear) {
printf("%d ", front1->info);
front1 = front1->ptr;
}
if (front1 == rear)
printf("%d", front1->info);
}

/* Dequeing the queue */


void deq() {
front1 = front;

if (front1 == NULL) {
printf("\n Error: Trying to display elements from empty queue");
return;
} else if (front1->ptr != NULL) {
front1 = front1->ptr;
printf("\n Dequed value : %d", front->info);
free(front);
front = front1;
} else {

Sateesh Kumar YNV Page 42


Data Structures LAB Manual R20 2021

printf("\n Dequed value : %d", front->info);


free(front);
front = NULL;
rear = NULL;
}
count--;
}

/* Returns the front element of queue */


int frontelement() {
if ((front != NULL) && (rear != NULL))
return(front->info);
else
return 0;
}

/* Display if queue is empty or not */


void empty() {
if ((front == NULL) && (rear == NULL))
printf("\n Queue empty");
else
printf("Queue not empty");
}

Output:

1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 3

No front element in Queue as queue is empty


Enter choice : 1
Enter data : 65

Enter choice : 1
Enter data : 95

Enter choice : 1

Sateesh Kumar YNV Page 43


Data Structures LAB Manual R20 2021

Enter data : 64

Enter choice : 6
65 95 64
Enter choice : 3
Front element : 65
Enter choice : 7

Queue size : 3
Enter choice : 4
Queue not empty
Enter choice : 1
Enter data : 46

Enter choice : 1
Enter data : 27

Enter choice : 6
65 95 64 46 27
Enter choice : 2

Dequed value : 65
Enter choice : 2

Dequed value : 95
Enter choice : 4
Queue not empty
Enter choice : 5

--------------------------------
Process exited after 71.4 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 44


Data Structures LAB Manual R20 2021

Exercise -6(Stack)
a) Write C program that implement stack (its operations) using arrays
Program:
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main() {
//clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do {
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice) {
case 1: {
push();
break;
}
case 2: {
pop();
break;
}
case 3: {
display();
break;
}
case 4: {
printf("\n\t EXIT POINT ");
break;
}
default: {
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}

}
} while(choice!=4);
return 0;

Sateesh Kumar YNV Page 45


Data Structures LAB Manual R20 2021

}
void push() {
if(top>=n-1) {
printf("\n\tSTACK is over flow");

} else {
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop() {
if(top<=-1) {
printf("\n\t Stack is under flow");
} else {
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display() {
if(top>=0) {
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
} else {
printf("\n The STACK is empty");
}

}
Output:

Enter the size of STACK[MAX=100]:10

STACK OPERATIONS USING ARRAY


--------------------------------
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter the Choice:1
Enter a value to be pushed:65

Sateesh Kumar YNV Page 46


Data Structures LAB Manual R20 2021

Enter the Choice:1


Enter a value to be pushed:65

Enter the Choice:1


Enter a value to be pushed:32

Enter the Choice:1


Enter a value to be pushed:48

Enter the Choice:3

The elements in STACK

48
32
65
65
Press Next Choice
Enter the Choice:2
The popped elements is 48
Enter the Choice:1
Enter a value to be pushed:46
Enter the Choice:3
The elements in STACK

46
32
65
65
Press Next Choice
Enter the Choice:2

The popped elements is 46


Enter the Choice:2

The popped elements is 32


Enter the Choice:2

The popped elements is 65


Enter the Choice:2

The popped elements is 65

Sateesh Kumar YNV Page 47


Data Structures LAB Manual R20 2021

Enter the Choice:22

Please Enter a Valid Choice(1/2/3/4)


Enter the Choice:2
Stack is under flow
Enter the Choice:4
EXIT POINT
--------------------------------
Process exited after 52.79 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 48


Data Structures LAB Manual R20 2021

Exercise -6(Stack)
b) Write C program that implement stack (its operations) using Linked list
Program:
/*
* C Program to Implement a Stack using Linked List
*/
#include <stdio.h>
#include <stdlib.h>

struct node {
int info;
struct node *ptr;
}*top,*top1,*temp;

int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();

int count = 0;

void main() {
int no, ch, e;

printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");

create();

while (1) {
printf("\n Enter choice : ");
scanf("%d", &ch);

Sateesh Kumar YNV Page 49


Data Structures LAB Manual R20 2021

switch (ch) {
case 1:
printf("Enter data : ");
scanf("%d", &no);
push(no);
break;
case 2:
pop();
break;
case 3:
if (top == NULL)
printf("No elements in stack");
else {
e = topelement();
printf("\n Top element : %d", e);
}
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
stack_count();
break;
case 8:
destroy();
break;
default :
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}

/* Create empty stack */


void create() {
top = NULL;
}

Sateesh Kumar YNV Page 50


Data Structures LAB Manual R20 2021

/* Count stack elements */


void stack_count() {
printf("\n No. of elements in stack : %d", count);
}

/* Push data into stack */


void push(int data) {
if (top == NULL) {
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
} else {
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}

/* Display stack elements */


void display() {
top1 = top;

if (top1 == NULL) {
printf("Stack is empty");
return;
}

while (top1 != NULL) {


printf("%d ", top1->info);
top1 = top1->ptr;
}
}

/* Pop Operation on stack */


void pop() {
top1 = top;

if (top1 == NULL) {
printf("\n Error : Trying to pop from empty stack");
return;
} else

Sateesh Kumar YNV Page 51


Data Structures LAB Manual R20 2021

top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}

/* Return top element */


int topelement() {
return(top->info);
}

/* Check if stack is empty or not */


void empty() {
if (top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}

/* Destroy entire stack */


void destroy() {
top1 = top;

while (top1 != NULL) {


top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;

printf("\n All stack elements destroyed");


}
Output:
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count

Sateesh Kumar YNV Page 52


Data Structures LAB Manual R20 2021

8 - Destroy stack
Enter choice : 1
Enter data : 65

Enter choice : 1
Enter data : 32

Enter choice : 1
Enter data : 46

Enter choice : 3

Top element : 46
Enter choice : 6
46 32 65
Enter choice : 7

No. of elements in stack : 3


Enter choice : 8

All stack elements destroyed


Enter choice : 6
Stack is empty
Enter choice : 1
Enter data : 95

Enter choice : 1
Enter data : 7
Enter choice : 1
Enter data : 62
Enter choice : 3
Top element : 62
Enter choice : 2

Popped value : 62
Enter choice : 4

Stack is not empty with 5 elements


Enter choice : 5

--------------------------------
Process exited after 48.53 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 53


Data Structures LAB Manual R20 2021

Exercise -6(Stack)
c) Write a C program that uses Stack operations to evaluate postfix expression
Program:
#include<stdio.h>
int stack[20];
int top = -1;

void push(int x) {
stack[++top] = x;
}

int pop() {
return stack[top--];
}

int main() {
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e = exp;
while(*e != '\0') {
if(isdigit(*e)) {
num = *e - 48;
push(num);
} else {
n1 = pop();
n2 = pop();
switch(*e) {
case '+': {
n3 = n1 + n2;
break;
}
case '-': {
n3 = n2 - n1;
break;
}
case '*': {
n3 = n1 * n2;
break;
}
case '/': {

Sateesh Kumar YNV Page 54


Data Structures LAB Manual R20 2021

n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
Output:
Enter the expression :: 23+4/

The result of expression 23+4/ = 1

--------------------------------
Process exited after 5.371 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 55


Data Structures LAB Manual R20 2021

Exercise -7(Binary Tree)


d) Write a recursive C program for traversing a binary tree in preorder, inorder and
postorder.
Program:
#include <stdio.h>
struct tnode {
int data;
struct tnode *right;
struct tnode *left;
};
struct tnode *CreateBST(struct tnode *, int);
void Inorder(struct tnode *);
void Preorder(struct tnode *);
void Postorder(struct tnode *);

int main() {
struct tnode *root = NULL;
int choice, item, n, i;
do {
printf("\n\nBinary Search Tree Operations\n");
printf("\n1. Creation of BST");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice) {
case 1:
root = NULL;
printf("\n\nBST for How Many Nodes ? ");
scanf("%d",&n);
for(i = 1; i <= n; i++) {
printf("\nEnter data for node %d : ", i);
scanf("%d",&item);
root = CreateBST(root,item);
}
printf("\nBST with %d nodes is ready to Use!!\n", n);
break;
case 2:
printf("\nBST Traversal in INORDER \n");
Inorder(root);
break;

Sateesh Kumar YNV Page 56


Data Structures LAB Manual R20 2021

case 3:
printf("\nBST Traversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nBST Traversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while(choice != 5);
return 0;
}

struct tnode *CreateBST(struct tnode *root, int item) {


if(root == NULL) {
root = (struct tnode *)malloc(sizeof(struct tnode));
root->left = root->right = NULL;
root->data = item;
return root;
} else {
if(item < root->data )
root->left = CreateBST(root->left,item);
else if(item > root->data )
root->right = CreateBST(root->right,item);
else
printf(" Duplicate Element !! Not Allowed !!!");

return(root);
}
}

void Inorder(struct tnode *root) {


if( root != NULL) {
Inorder(root->left);
printf(" %d ",root->data);
Inorder(root->right);
}

Sateesh Kumar YNV Page 57


Data Structures LAB Manual R20 2021

void Preorder(struct tnode *root) {


if( root != NULL) {
printf(" %d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
}

void Postorder(struct tnode *root) {


if( root != NULL) {
Postorder(root->left);
Postorder(root->right);
printf(" %d ",root->data);
}
}
Output:

Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 1

BST for How Many Nodes ? 6

Enter data for node 1 : 32

Enter data for node 2 : 15

Enter data for node 3 : 65

Enter data for node 4 : 9

Enter data for node 5 : 15


Duplicate Element !! Not Allowed !!!

Sateesh Kumar YNV Page 58


Data Structures LAB Manual R20 2021

Enter data for node 6 : 48

BST with 6 nodes is ready to Use!!

Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 2

BST Traversal in INORDER


9 15 32 48 65

Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 3

BST Traversal in PREORDER


32 15 9 65 48

Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 4

BST Traversal in POSTORDER


9 15 48 65 32

Sateesh Kumar YNV Page 59


Data Structures LAB Manual R20 2021

Binary Search Tree Operations

1. Creation of BST
2. Traverse in Inorder
3. Traverse in Preorder
4. Traverse in Postorder
5. Exit

Enter Choice : 5

Terminating

--------------------------------
Process exited after 48.98 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 60


Data Structures LAB Manual R20 2021

Exercise -8(Binary Search Tree)


a) Write a C program to Create a BST
b) Write a C program to insert a node into a BST.
c) Write a C program to delete a node from a BST.
Program:
/*
* C Program to Construct a Binary Search Tree and perform deletion, inorder traversal on it
*/
#include <stdio.h>
#include <stdlib.h>

struct btnode {
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;

void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);

int flag = 1;

void main() {
int ch;

printf("\nOPERATIONS ---");
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1) {
printf("\nEnter your choice : ");

Sateesh Kumar YNV Page 61


Data Structures LAB Manual R20 2021

scanf("%d", &ch);
switch (ch) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}

/* To insert a node in the tree */


void insert() {
create();
if (root == NULL)
root = temp;
else
search(root);
}

/* To create a node */
void create() {
int data;

printf("Enter data of node to be inserted : ");


scanf("%d", &data);
temp = (struct btnode *)malloc(1*sizeof(struct btnode));
temp->value = data;

Sateesh Kumar YNV Page 62


Data Structures LAB Manual R20 2021

temp->l = temp->r = NULL;


}

/* Function to search the appropriate position to insert the new node */


void search(struct btnode *t) {
if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value
insert at right */
search(t->r);
else if ((temp->value > t->value) && (t->r == NULL))
t->r = temp;
else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node
value insert at left */
search(t->l);
else if ((temp->value < t->value) && (t->l == NULL))
t->l = temp;
}

/* recursive function to perform inorder traversal of tree */


void inorder(struct btnode *t) {
if (root == NULL) {
printf("No elements in a tree to display");
return;
}
if (t->l != NULL)
inorder(t->l);
printf("%d -> ", t->value);
if (t->r != NULL)
inorder(t->r);
}

/* To check for the deleted node */


void delete() {
int data;

if (root == NULL) {
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d", &data);
t1 = root;
t2 = root;
search1(root, data);

Sateesh Kumar YNV Page 63


Data Structures LAB Manual R20 2021

/* To find the preorder traversal */


void preorder(struct btnode *t) {
if (root == NULL) {
printf("No elements in a tree to display");
return;
}
printf("%d -> ", t->value);
if (t->l != NULL)
preorder(t->l);
if (t->r != NULL)
preorder(t->r);
}

/* To find the postorder traversal */


void postorder(struct btnode *t) {
if (root == NULL) {
printf("No elements in a tree to display ");
return;
}
if (t->l != NULL)
postorder(t->l);
if (t->r != NULL)
postorder(t->r);
printf("%d -> ", t->value);
}

/* Search for the appropriate position to insert the new node */


void search1(struct btnode *t, int data) {
if ((data>t->value)) {
t1 = t;
search1(t->r, data);
} else if ((data < t->value)) {
t1 = t;
search1(t->l, data);
} else if ((data==t->value)) {
delete1(t);
}
}

/* To delete a node */
void delete1(struct btnode *t) {

Sateesh Kumar YNV Page 64


Data Structures LAB Manual R20 2021

int k;

/* To delete leaf node */


if ((t->l == NULL) && (t->r == NULL)) {
if (t1->l == t) {
t1->l = NULL;
} else {
t1->r = NULL;
}
t = NULL;
free(t);
return;
}

/* To delete node having one left hand child */


else if ((t->r == NULL)) {
if (t1 == t) {
root = t->l;
t1 = root;
} else if (t1->l == t) {
t1->l = t->l;

} else {
t1->r = t->l;
}
t = NULL;
free(t);
return;
}

/* To delete node having right hand child */


else if (t->l == NULL) {
if (t1 == t) {
root = t->r;
t1 = root;
} else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}

Sateesh Kumar YNV Page 65


Data Structures LAB Manual R20 2021

/* To delete node having two child */


else if ((t->l != NULL) && (t->r != NULL)) {
t2 = root;
if (t->r != NULL) {
k = smallest(t->r);
flag = 1;
} else {
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}

/* To find the smallest element in the right sub tree */


int smallest(struct btnode *t) {
t2 = t;
if (t->l != NULL) {
t2 = t;
return(smallest(t->l));
} else
return (t->value);
}

/* To find the largest element in the left sub tree */


int largest(struct btnode *t) {
if (t->r != NULL) {
t2 = t;
return(largest(t->r));
} else
return(t->value);
}
Output:

OPERATIONS ---
1 - Insert an element into tree
2 - Delete an element from the tree
3 - Inorder Traversal
4 - Preorder Traversal
5 - Postorder Traversal

Sateesh Kumar YNV Page 66


Data Structures LAB Manual R20 2021

6 - Exit

Enter your choice : 1


Enter data of node to be inserted : 65

Enter your choice : 1


Enter data of node to be inserted : 62

Enter your choice : 1


Enter data of node to be inserted : 25

Enter your choice : 1


Enter data of node to be inserted : 14

Enter your choice : 1


Enter data of node to be inserted : 32

Enter your choice : 3


14 -> 25 -> 32 -> 62 -> 65 ->
Enter your choice : 4
65 -> 62 -> 25 -> 14 -> 32 ->
Enter your choice : 5
14 -> 32 -> 25 -> 62 -> 65 ->
Enter your choice : 2
Enter the data to be deleted : 14

Enter your choice : 3


25 -> 32 -> 62 -> 65 ->
Enter your choice : 2
Enter the data to be deleted : 65

Enter your choice : 3


25 -> 32 -> 62 ->
Enter your choice : 5
32 -> 25 -> 62 ->
Enter your choice : 6

--------------------------------
Process exited after 83.36 seconds with return value 0
Press any key to continue . . .

Sateesh Kumar YNV Page 67

You might also like