DS Lab Record PDF

You might also like

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

VIGNAN’S FOUNDATION FOR SCIENCE, TECHNOLOGY AND RESEARCH

(Deemed to be UNIVERSITY)
VADLAMUDI – 522 213, GUNTUR DIST, ANDHRA PRADESH, INDIA.

Department of Information Technology

19CS202 DATA STRCTURES


LAB RECORD

Name :____________________
Register Number :____________________
Section :____________________
Department of
INFORMATION TECHNOLOGY

CERTIFICATE

This is to certify that Mr./Ms.______________________________________ bearing Regd. No.

_____________________ is a student of B.Tech 2-1 Semester has successfully completed

____________ experiments in ___________________________ Lab during the academic year

2020-2021.

Faculty In charge Head of the Department

External Examiner
INDEX

Name of the Lab: 19CS202 Data Structures

S. Date Title of the Experiment Page Marks &


No No. Sign
1a. Write a C program to implement the Selection Sorting methods to arrange a given list of data
items (number of data items >=5000) in ascending/descending order.

Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
int smallest(int arr[], int k, int n);
void selection_sort(int arr[], int n);
int main()
{
int arr[10], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
return 0;
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}

Output:
1b. Write a C program to implement the Insertion Sorting methods to arrange a given list of data
items (number of data items >=5000) in ascending/descending order.

Algorithm:
Coding:
#include <stdio.h>
#define size 50
void insertion_sort(int arr[], int n);
int main()
{
int arr[size], i, n;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements of the array: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
insertion_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
return 0;
}
void insertion_sort(int arr[], int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = arr[i];
j = i-1;
while((temp < arr[j]) && (j>=0))
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
Output:
1c. Write a C program to implement the Bubble Sorting methods to arrange a given list of data
items (number of data items >=5000) in ascending/descending order.

Algorithm:
Coding:
#include <stdio.h>
int main()
{
int i, n, temp, j, arr[10];
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr [i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("\n The array sorted in ascending order is :\n");
for(i=0;i<n;i++)
printf("%d\t", arr[i]);
return 0;
}
Output:
1d. Measure the performance of each of the above sorting technique and compare with their
theoretical time complexities.
2a. Write a C program to implement the Linear searching techniques on a given list of data items
organized in the form of array.

Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#define size 20
int main()
{
int arr[size], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at position= %d", num,i+1);
/* +1 added in line 23 so that it would display the number in
the first place in the array as in position 1 instead of 0 */
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
Output:
2b. Write a C program to implement the Binary searching techniques on a given list of data items
organized in the form of array.

Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#define size 10
int smallest(int arr[], int k, int n);
void selection_sort(int arr[], int n);
int main()
{
int arr[size], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n);
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position %d", num, mid+1);
found =1;
break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
Output:
2c. Analyze the performance of each searching technique and write down your observations.
3a. Write a C program to Create a Singly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main(int argc, char *argv[])
{
int option;
do
{
printf("\n\n *****MAIN MENU ****");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
}
}while(option !=3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
Output:
3b. Write a C program to Count the number of nodes in SLL
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
int count=0;
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main(int argc, char *argv[])
{
int option;
do
{
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
}
}while(option !=3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
count++;
}
printf("\n Number of nodes in this Singly linked list is %d", count);
return start;
}
Output:
3c. Write a C program to perform Insertion and Deletion operations at Front, at end and at a given
position in a Singly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *insert_before(struct node *);
struct node *insert_after(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_node(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
int main(int argc, char *argv[])
{
int option;
do
{
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Add a node before a given node");
printf("\n 6: Add a node after a given node");
printf("\n 7: Delete a node from the beginning");
printf("\n 8: Delete a node from the end");
printf("\n 9: Delete a given node");
printf("\n 10: Delete a node after a given node");
printf("\n 11: Delete the entire list");
printf("\n 12: EXIT");
printf("\n\n Enter your option :");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start = delete_beg(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete_node(start);
break;
case 10: start = delete_after(start);
break;
case 11: start = delete_list(start);
printf("\n LINKED LIST DELETED");
break;
}
}while(option!=12);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data :" );
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf("\n Enter the data :" );
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = start;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
new_node -> next = NULL;
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
return start;
}
struct node *insert_before(struct node *start)
{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data :");
scanf("%d", &num);
printf("\n Enter the value before which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = new_node;
new_node -> next = ptr;
return start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr, *preptr;
int num, val;
printf("\n Enter the data :" );
scanf("%d", &num);
printf("\n Enter the value after which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=new_node;
new_node -> next = ptr;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start -> next;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr, *preptr;
ptr = start;
while(ptr -> next != NULL)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = NULL;
free(ptr);
return start;
}
struct node *delete_node(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value of the node which has to be deleted :" );
scanf("%d", &val);
ptr = start;
if(ptr -> data == val)
{
start = delete_beg(start);
return start;
}
else
{
while(ptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next = ptr -> next;
free(ptr);
return start;
}
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *preptr;
int val;
printf("\n Enter the value after which the node has to deleted : ");
scanf("%d", &val);
ptr = start;
preptr = ptr;
while(preptr -> data != val)
{
preptr = ptr;
ptr = ptr -> next;
}
preptr -> next=ptr -> next;
free(ptr);
return start;
}
struct node *delete_list(struct node *start)
{
struct node *ptr;
if(start!=NULL)
{
ptr=start;
while(ptr != NULL)
{
printf("\n %d is to be deleted next", ptr -> data);
start = delete_beg(ptr);
ptr = start;
}
}
return start;
}
Output:
3d. Write a C program to perform Traversal operation in a Singly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main(int argc, char *argv[])
{
int option;
do
{
printf("\n 1: Create a list");
printf("\n 2: Traversal");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
}
}while(option !=3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr != NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
Output:
3e. Write a C program to Search a given element (KEY) in SLL
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *search(struct node *);
int main(int argc, char *argv[])
{
int option;
do
{
printf("\n 1: Create a list");
printf("\n 2: Search an element in the list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n LINKED LIST CREATED");
break;
case 2: start = search(start);
break;
}
}while(option !=3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> data=num;
if(start==NULL)
{
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
while(ptr->next!=NULL)
ptr=ptr->next;
ptr->next = new_node;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *search(struct node *start)
{
struct node *ptr;
int num,found=0;
ptr = start;
printf("\n Enter the number to be searched:");
scanf("%d",&num);
while(ptr -> next != NULL)
{
if(ptr->data == num)
{
found =1;
printf("\n %d is found in this SLL\n", num);
}
ptr=ptr->next;
}
if(ptr->data==num)
{
found=1;
printf("\n %d is in the last node of SLL\n ",num);
}
if (found == 0)
printf("\n %d does not exist in the SLL\n", num);
return start;
}
Output:
4a. Write a C program to Create a Doubly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter –1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
printf("\n");
while(ptr!=NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}

Output:
4b. Write a C program to Count the number of nodes in DLL
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *count(struct node *);
int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Number of Nodes in this doubly linked list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = count(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter –1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *count(struct node *start)
{
struct node *ptr;
int count=0;
ptr=start;
while(ptr!=NULL)
{
count++;
ptr = ptr -> next;
}
printf("\n Number of Nodes in this doubly linked list is: %d",count);
return start;
}
Output:
4c. Write a C program to perform Insertion and Deletion operations at Front, at end and at a given
position in a Doubly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *display(struct node *);
struct node *insert_beg(struct node *);
struct node *insert_end(struct node *);
struct node *insert_before(struct node *);
struct node *insert_after(struct node *);
struct node *delete_beg(struct node *);
struct node *delete_end(struct node *);
struct node *delete_before(struct node *);
struct node *delete_after(struct node *);
struct node *delete_list(struct node *);
int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Display the list");
printf("\n 3: Add a node at the beginning");
printf("\n 4: Add a node at the end");
printf("\n 5: Add a node before a given node");
printf("\n 6: Add a node after a given node");
printf("\n 7: Delete a node from the beginning");
printf("\n 8: Delete a node from the end");
printf("\n 9: Delete a node before a given node");
printf("\n 10: Delete a node after a given node");
printf("\n 11: Delete the entire list");
printf("\n 12: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = display(start);
break;
case 3: start = insert_beg(start);
break;
case 4: start = insert_end(start);
break;
case 5: start = insert_before(start);
break;
case 6: start = insert_after(start);
break;
case 7: start = delete_beg(start);
break;
case 8: start = delete_end(start);
break;
case 9: start = delete_before(start);
break;
case 10:start = delete_after(start);
break;
case 11:start = delete_list(start);
printf("\n DOUBLY LINKED LIST DELETED");
break;
}
}while(option != 12);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter -1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num != -1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *display(struct node *start)
{
struct node *ptr;
ptr=start;
printf("\n");
while(ptr!=NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
struct node *insert_beg(struct node *start)
{
struct node *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
start -> prev = new_node;
new_node -> next = start;
new_node -> prev = NULL;
start = new_node;
return start;
}
struct node *insert_end(struct node *start)
{
struct node *ptr, *new_node;
int num;
printf("\n Enter the data : ");
scanf("%d", &num);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr=start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> next = new_node;
new_node -> prev = ptr;
new_node -> next = NULL;
return start;
}
struct node *insert_before(struct node *start)
{
struct node *new_node, *ptr;
int num, val;
printf("\n Enter the data : ");
scanf("%d", &num);
printf("\n Enter the value before which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val)
ptr = ptr -> next;
new_node -> next = ptr;
new_node -> prev = ptr-> prev;
ptr -> prev -> next = new_node;
ptr -> prev = new_node;
return start;
}
struct node *insert_after(struct node *start)
{
struct node *new_node, *ptr;
int num, val;
printf("\n Enter the data : ");
scanf("%d", &num);
printf("\n Enter the value after which the data has to be inserted : ");
scanf("%d", &val);
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = num;
ptr = start;
while(ptr -> data != val)
ptr = ptr -> next;
new_node -> prev = ptr;
new_node -> next = ptr -> next;
ptr -> next -> prev = new_node;
ptr -> next = new_node;
return start;
}
struct node *delete_beg(struct node *start)
{
struct node *ptr;
ptr = start;
start = start -> next;
start -> prev = NULL;
free(ptr);
return start;
}
struct node *delete_end(struct node *start)
{
struct node *ptr;
ptr = start;
while(ptr -> next != NULL)
ptr = ptr -> next;
ptr -> prev -> next = NULL;
free(ptr);
return start;
}
struct node *delete_after(struct node *start)
{
struct node *ptr, *temp;
int val;
printf("\n Enter the value after which the node has to deleted : ");
scanf("%d", &val);
ptr = start;
while(ptr -> data != val)
ptr = ptr -> next;
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
return start;
}
struct node *delete_before(struct node *start)
{
struct node *ptr, *temp;
int val;
printf("\n Enter the value before which the node has to deleted : ");
scanf("%d", &val);
ptr = start;
while(ptr -> data != val)
ptr = ptr -> next;
temp = ptr -> prev;
if(temp == start)
start = delete_beg(start);
else
{
ptr -> prev = temp -> prev;
temp -> prev -> next = ptr;
}
free(temp);
return start;
}
struct node *delete_list(struct node *start)
{
while(start != NULL)
start = delete_beg(start);
return start;
}
Output:
4d. Write a C program to perform Traversal operation in a Doubly linked list
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *traversal(struct node *);
int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Traversal to all nodes");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = traversal(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter –1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *traversal(struct node *start)
{
struct node *ptr;
ptr=start;
while(ptr!=NULL)
{
printf("\t %d", ptr -> data);
ptr = ptr -> next;
}
return start;
}
Output:
4e. Write a C program to Search a given element (KEY) in DLL
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
struct node *next;
int data;
struct node *prev;
};
struct node *start = NULL;
struct node *create_ll(struct node *);
struct node *search(struct node *);
int main()
{
int option;
do
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a list");
printf("\n 2: Search an element in the list");
printf("\n 3: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_ll(start);
printf("\n DOUBLY LINKED LIST CREATED");
break;
case 2: start = search(start);
break;
}
}while(option != 3);
return 0;
}
struct node *create_ll(struct node *start)
{
struct node *new_node, *ptr;
int num;
printf("\n Enter –1 to end");
printf("\n Enter the data : ");
scanf("%d", &num);
while(num!=-1)
{
if(start == NULL)
{
new_node = (struct node*)malloc(sizeof(struct node));
new_node -> prev = NULL;
new_node -> data = num;
new_node -> next = NULL;
start = new_node;
}
else
{
ptr=start;
new_node = (struct node*)malloc(sizeof(struct node));
new_node->data=num;
while(ptr->next!=NULL)
ptr = ptr->next;
ptr->next = new_node;
new_node->prev=ptr;
new_node->next=NULL;
}
printf("\n Enter the data : ");
scanf("%d", &num);
}
return start;
}
struct node *search(struct node *start)
{
struct node *ptr;
int num,found=0;
ptr = start;
printf("\n Enter the number to be searched:");
scanf("%d",&num);
while(ptr -> next != NULL)
{
if(ptr->data == num)
{
found =1;
printf("\n %d is found in this DLL\n", num);
}
ptr=ptr->next;
}
if(ptr->data==num)
{
found=1;
printf("\n %d is in the last node of DLL\n ",num);
}
if (found == 0)
printf("\n %d does not exist in the DLL\n", num);
return start;
}
Output:
5a. Write a C program to implement the STACK using an array and linked list and perform Push
an Element on to Stack operations.

Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
int st[MAX], top=-1;
void push(int st[], int val);
void display(int st[]);
int main(int argc, char *argv[])
{
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. DISPLAY");
printf("\n 3. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2: display(st);
break;
}
}while(option != 3);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
Output:
5b. Write a C program to implement the STACK using an array and linked list and perform Pop
an Element from Stack operations.

Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#define MAX 3
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
void display(int st[]);
int main(int argc, char *argv[])
{
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2: val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3: display(st);
break;
}
}while(option != 4);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
Output:
5c. Demonstrate how Stack can be used to check given string is Palindrome
Algorithm:
Coding:
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* stack;
int top = -1;
void push(char ele)
{
stack[++top] = ele;
}
char pop()
{
return stack[top--];
}
int isPalindrome(char str[])
{
int length = strlen(str);
int i, mid = length / 2;
stack = (char*)malloc(length * sizeof(char));
for (i = 0; i < mid; i++) {
push(str[i]);
}
if (length % 2 != 0)
{
i++;
}
while (str[i] != '\0') {
char ele = pop();

if (ele != str[i])
return 0;
i++;
}
return 1;
}
int main()
{
char str[20];
printf("Enter the string to check whether palindrome or not : ");
scanf("%s",str);
if (isPalindrome(str))
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
Output:
5d. Demonstrate Overflow and Underflow situations on Stack
Algorithm:
Coding:
#include <stdio.h>
#include <stdlib.h>
#define MAX 2
int st[MAX], top=-1;
void push(int st[], int val);
int pop(int st[]);
void display(int st[]);
int main(int argc, char *argv[])
{
int val, option;
do
{
printf("\n *****MAIN MENU*****");
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. DISPLAY");
printf("\n 4. EXIT");
printf("\n Enter your option: ");
scanf("%d", &option);
switch(option)
{
case 1: printf("\n Enter the number to be pushed on stack: ");
scanf("%d", &val);
push(st, val);
break;
case 2: val = pop(st);
if(val != -1)
printf("\n The value deleted from stack is: %d", val);
break;
case 3: display(st);
break;
}
}while(option != 4);
return 0;
}
void push(int st[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else
{
top++;
st[top] = val;
}
}
int pop(int st[])
{
int val;
if(top == -1)
{
printf("\n STACK UNDERFLOW");
return -1;
}
else
{
val = st[top];
top--;
return val;
}
}
void display(int st[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else
{
for(i=top;i>=0;i--)
printf("\n %d",st[i]);
printf("\n");
}
}
Output:
6. Write a C program to evaluate POSTFIX expression using STACK.
Algorithm:
Coding:
#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 '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}
Output:
7a. Write a C Program to implement Queue by using an array and linked list and perform Enqueue
(add element to end of queue) operation.

Algorithm:
Coding:
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n 1. Insert an element");
printf("\n 2. Display the queue");
printf("\n 3. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: insert();
break;

case 2: display();
break;
}
}while(option != 3);
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue : ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i <= rear;i++)
printf("\t %d", queue[i]);
}
}
Output:
7b. Write a C Program to implement Queue by using an array and linked list and perform
Dequeue (remove element from front of queue) operation.

Algorithm:
Coding:
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n\n ***** MAIN MENU *****");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Display the queue");
printf("\n 4. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: insert();
break;
case 2: val = delete_element();
if (val != -1)
printf("\n The number deleted is : %d", val);
break;
case 3: display();
break;
}
}while(option != 4);
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue : ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i <= rear;i++)
printf("\t %d", queue[i]);
}
}
Output:
7c. Write a C Program to implement Queue by using an array and linked list and perform IsEmpty
(check if queue is empty) operation.

Algorithm:
Coding:
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
int delete_element(void);
void isempty(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n\n ***** MAIN MENU *****");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Check Queue is emptry or not");
printf("\n 4. Display the queue");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: insert();
break;
case 2: val = delete_element();
if (val != -1)
printf("\n The number deleted is : %d", val);
break;
case 3: isempty();
break;
case 4: display();
break;
}
}while(option != 5);
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue : ");
scanf("%d", &num);
if(rear == MAX-1)
printf("\n OVERFLOW");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
int delete_element()
{
int val;
if(front == -1 || front>rear)
{
printf("\n UNDERFLOW");
return -1;
}
else
{
val = queue[front];
front++;
if(front > rear)
front = rear = -1;
return val;
}
}
void isempty()
{
if(front==-1 || front>rear)
{
printf("\n QUEUE IS EMPTY");
}
else
{
printf("\n QUEUE IS NOT EMPTY");
}
}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i <= rear;i++)
printf("\t %d", queue[i]);
}
}
Output:
7d. Write a C Program to implement Queue by using an array and linked list and perform IsFull
(Check if queue is full) operation.

Algorithm:
Coding:
#include <stdio.h>
#define MAX 2
int queue[MAX];
int front = -1, rear = -1;
void insert(void);
void isfull(void);
void display(void);
int main()
{
int option, val;
do
{
printf("\n\n ***** MAIN MENU *****");
printf("\n 1. Insert an element");
printf("\n 2. isfull");
printf("\n 3. Display the queue");
printf("\n 4. EXIT");
printf("\n Enter your option : ");
scanf(" %d", &option);
switch(option)
{
case 1: insert();
break;
case 2: isfull();
break;
case 3: display();
break;
}
}while(option != 4);
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue : ");
scanf(" %d", &num);
if(rear == MAX-1)
printf("\n ");
else if(front == -1 && rear == -1)
front = rear = 0;
else
rear++;
queue[rear] = num;
}
void isfull()
{
if(rear == MAX-1)
printf("\n Queue is full");
else
printf("\n Queue is not full");

}
void display()
{
int i;
printf("\n");
if(front == -1 || front > rear)
printf("\n QUEUE IS EMPTY");
else
{
for(i = front;i <= rear;i++)
printf("\t %d", queue[i]);
}
}
Output:
8a. Write a C Program to Create a Binary Search Tree(BST) of N Integers: 6, 9, 5, 2, 8, 15, 24,
14, 7, 8, 5, 2
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *, int);
void preorderTraversal(struct node *);
void inorderTraversal(struct node *);
void postorderTraversal(struct node *);
int main()
{
int option, val;
struct node *ptr;
create_tree(tree);
do
{
printf("\n ******MAIN MENU******* \n");
printf("\n 1. Insert Element");
printf("\n 2. Preorder Traversal");
printf("\n 3. Inorder Traversal");
printf("\n 4. Postorder Traversal");
printf("\n 5. Exit");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree, val);
break;
case 2:
printf("\n The elements of the tree are : \n");
preorderTraversal(tree);
break;
case 3:
printf("\n The elements of the tree are : \n");
inorderTraversal(tree);
break;
case 4:
printf("\n The elements of the tree are : \n");
postorderTraversal(tree);
break;

}
}while(option!=5);
return 0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(val<parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
void preorderTraversal(struct node *tree)
{
if(tree != NULL)
{
printf("%d\t", tree->data);
preorderTraversal(tree->left);
preorderTraversal(tree->right);
}
}
void inorderTraversal(struct node *tree)
{
if(tree != NULL)
{
inorderTraversal(tree->left);
printf("%d\t", tree->data);
inorderTraversal(tree->right);
}
}
void postorderTraversal(struct node *tree)
{
if(tree != NULL)
{
postorderTraversal(tree->left);
postorderTraversal(tree->right);
printf("%d\t", tree->data);
}
}
Output:
8b. Write a C Program to Traverse the BST in In order, Preorder and Post Order
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *, int);
void preorderTraversal(struct node *);
void inorderTraversal(struct node *);
void postorderTraversal(struct node *);
int main()
{
int option, val;
struct node *ptr;
create_tree(tree);
do
{
printf("\n ******MAIN MENU******* \n");
printf("\n 1. Insert Element");
printf("\n 2. Preorder Traversal");
printf("\n 3. Inorder Traversal");
printf("\n 4. Postorder Traversal");
printf("\n 5. Exit");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree, val);
break;
case 2:
printf("\n The elements of the tree are : \n");
preorderTraversal(tree);
break;
case 3:
printf("\n The elements of the tree are : \n");
inorderTraversal(tree);
break;
case 4:
printf("\n The elements of the tree are : \n");
postorderTraversal(tree);
break;

}
}while(option!=5);
return 0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(val<parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
void preorderTraversal(struct node *tree)
{
if(tree != NULL)
{
printf("%d\t", tree->data);
preorderTraversal(tree->left);
preorderTraversal(tree->right);
}
}
void inorderTraversal(struct node *tree)
{
if(tree != NULL)
{
inorderTraversal(tree->left);
printf("%d\t", tree->data);
inorderTraversal(tree->right);
}
}
void postorderTraversal(struct node *tree)
{
if(tree != NULL)
{
postorderTraversal(tree->left);
postorderTraversal(tree->right);
printf("%d\t", tree->data);
}
}
Output:
8c. Write a C Program to Search a given element (KEY) in BST and report the appropriate
message.
Algorithm:
Coding:
#include <stdio.h>
#include <malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *tree;
void create_tree(struct node *);
struct node *insertElement(struct node *, int);
int searchvalue(struct node *,int);

int main()
{
int option, val,r;
struct node *ptr;
create_tree(tree);
do
{
printf("\n ******MAIN MENU******* \n");
printf("\n 1. Insert Element");
printf("\n 2. Search a Value in BST");
printf("\n 3. Exit");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
printf("\n Enter the value of the new node : ");
scanf("%d", &val);
tree = insertElement(tree, val);
break;
case 2:
printf("\n Enter the value to be searched : ");
scanf("%d", &val);
r=searchvalue(tree,val);
if(r!=3)
{
printf("\n value is not found");
}
break;
}
}while(option!=3);
return 0;
}
void create_tree(struct node *tree)
{
tree = NULL;
}
struct node *insertElement(struct node *tree, int val)
{
struct node *ptr, *nodeptr, *parentptr;
ptr = (struct node*)malloc(sizeof(struct node));
ptr->data = val;
ptr->left = NULL;
ptr->right = NULL;
if(tree==NULL)
{
tree=ptr;
tree->left=NULL;
tree->right=NULL;
}
else
{
parentptr=NULL;
nodeptr=tree;
while(nodeptr!=NULL)
{
parentptr=nodeptr;
if(val<nodeptr->data)
nodeptr=nodeptr->left;
else
nodeptr = nodeptr->right;
}
if(val<parentptr->data)
parentptr->left = ptr;
else
parentptr->right = ptr;
}
return tree;
}
int searchvalue(struct node *tree,int val)
{
if(tree != NULL)
{
if(tree->data==val)
{
printf("\n value is found");
return 3;
}
searchvalue(tree->left,val);
searchvalue(tree->right,val);
}
}
Output:
9a. Write C programs for implementing the Depth first search traversal techniques.
Algorithm:
Coding:
#include<stdio.h>

void DFS(int);
int G[10][10],v[10],n;
int main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);

printf("\nEnter adjecency matrix of the graph:\n");

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

for(i=0;i<n;i++)
v[i]=0;
printf("enter starting vertex \n");
scanf("%d",&i);
DFS(i);

return 0;
}

void DFS(int i)
{
int j;
printf("%d -> ",i);
v[i]=1;

for(j=0;j<n;j++)
if(!v[j]&&G[i][j]==1)
DFS(j);
}
Output:
9b. Write C programs for implementing the Breadth first search traversal techniques.
Algorithm:
Coding:
#include<stdio.h>
int a[20][20],q[20],visited[20],n,i,j,f=1,r=0;

void bfs(int v)
{
for (i=1;i<=n;i++)
{
if(a[v][i]==1 && visited[i]==0)
{
printf("%d -> %d \n",v,i);
q[++r]=i;
visited[i]=1;
}
}
if(f<=r)
bfs(q[f++]);
}
int main()
{
int v;
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the starting vertex:");
scanf("%d",&v);
visited[v]=1;
printf("\n The nodes which are reachable are:\n");
bfs(v);
return 0;
}
Output:
9c. Measure the time required to perform each traversal operation.
Viva-Voce Question
UNIT – I
1. Which technique of searching an element inan array would you prefer to use and in which
situation?
2. Define sorting. What is the importance of sorting?
3. What are the different types of sorting techniques?Which sorting technique has the least worst case?
4. Explain the difference between bubble sort andquick sort. Which one is more efficient?
5. Sort the elements 77, 49, 25, 12, 9, 33, 56, 81using
(a) insertion sort (b) selection sort(c) bubble sort (d) merge sort(e) quick sort (f) radix sort
(g) shell sort
6. Compare heap sort and quick sort.
7. Quick sort shows quadratic behaviour in certainsituations. Justify.
8. If the following sequence of numbers is to besorted using quick sort, then show the iterations
of the sorting process.42, 34, 75, 23, 21, 18, 90, 67, 78
9. Sort the following sequence of numbers indescending order using heap sort.
42, 34, 75, 23, 21, 18, 90, 67, 78
10. A certain sorting technique was applied to thefollowing data set,
45, 1, 27, 36, 54, 90
After two passes, the rearrangement of the dataset is given as below:
1, 27, 45, 36, 54, 90
Identify the sorting algorithm that was applied.
11. A certain sorting technique was applied to thefollowing data set,
81, 72, 63, 45, 27, 36
After two passes, the rearrangement of the dataset is given as below:
27, 36, 80, 72, 63, 45
Identify the sorting algorithm that was applied.
12. a certain sorting technique was applied to thefollowing data set,
45, 1, 63, 36, 54, 90
After two passes, the rearrangement of the dataset is given as below:
1, 45, 63, 36, 54, 90
Identify the sorting algorithm that was applied.
13. Write a recursive function to perform selectionsort.
14. Compare the running time complexity of differentsorting algorithms.
15. Discuss the advantages of insertion sort.
UNIT – II
1. Make a comparison between a linked list and alinear array. Which one will you prefer to use andwhen?
2. Why is a doubly linked list more useful than asingly linked list?
3. Give the advantages and uses of a circular linkedlist.
4. Specify the use of a header node in a header linkedlist.
5. Give the linked representation of the followingpolynomial:
7x3y2 – 8x2y + 3xy + 11x – 4
6. Explain the difference between a circular linkedlist and a singly linked list.
7. Form a linked list to store students’ details.
8. Use the linked list of the above question to insertthe record of a new student in the list.
9. Delete the record of a student with a specified rollnumber from the list maintained in Question 7.
10. Given a linked list that contains English alphabet.The characters may be in upper case or in lowercase.
Create two linked lists—one which storesupper case characters and the other that storeslower case characters.
tstoember
11. Create a linked list which stores names of theemployees. Then sort these names and re-displaythe contents
of the linked list.
UNIT – III
1. What do you understand by stack overflow andunderflow?
2. Differentiate between an array and a stack.
3. How does a stack implemented using a linked listdiffer from a stack implemented using an array?
4. Differentiate between peek() and pop() functions.
5. Why are parentheses not required in postfix/prefixexpressions?
6. Explain how stacks are used in a non-recursiveprogram?
7. What do you understand by a multiple stack? Howis it useful?
8. Explain the terms infix expression, prefixexpression, and postfix expression. Convert
the following infix expressions to their postfixequivalents:
(a) A – B + C (b) A * B + C / D
(c) (A – B ) + C * D / E – C
(d) (A * B) + (C / D) – ( D + E)
(e) ((A – B) + D / ((E + F) * G))
(f) ( A – 2 * (B + C) / D * E) + F
(g) 14 / 7 * 3 – 4 + 9 / 2
9. Convert the following infix expressions to theirpostfix equivalents:
(a) A – B + C (b) A * B + C / D
(c) (A – B ) + C * D / E – C
(d) (A * B) + (C / D) – ( D + E)
(e) ((A – B) + D / ((E + F) * G))
(f) ( A – 2 * (B + C) / D * E) + F
(g) 14 / 7 * 3 – 4 + 9 / 2
10. Find the infix equivalents of the following postfixequivalents:
(a) A B + C * D – (b) ABC * + D –
11. Give the infix expression of the following prefixexpressions.
(a) * – + A B C D (b) + – a * B C D
12. Convert the expression given below into itscorresponding postfix expression and then evaluateit. Also write
a program to evaluate a postfixexpression.
10 + ((7 – 5) + 10)/2
13. Write a function that accepts two stacks. Copy thecontents of first stack in the second stack. Note thatthe
order of elements must be preserved.(Hint: use a temporary stack)
14. Draw the stack structure in each case when the followingoperations are performed on an empty stack.
(a) Add A, B, C, D, E, F (b) Delete two letters(c) Add G (d) Add H
(e) Delete four letters (f) Add I
15. Differentiate between an iterative function and arecursive function. Which one will you prefer touse and in
what circumstances?
16. Explain the Tower of Hanoi problem.
1. What is a priority queue? Give its applications.
2. Explain the concept of a circular queue? How isit better than a linear queue?
3. Why do we use multiple queues?
4. Draw the queue structure in each case when thefollowing operations are performed on an emptyqueue.
(a) Add A, B, C, D, E, F(b) Delete two letters(c) Add G (d) Add H
(e) Delete four letters (f) Add I
5. Consider the queue given below which has FRONT= 1 and REAR = 5.

Now perform the following operations on thequeue:


(a) Add F (b) Delete two letters(c) Add G (d) Add H(e) Delete four letters (f) Add I
6. Consider the dequeue given below which has LEFT= 1 and RIGHT = 5.

Now perform the following operations on thequeue:


(a) Add F on the left(b) Add G on the right(c) Add H on the right(d) Delete two letters from left(e) Add I on the
right(f) Add J on the left(g) Delete two letters from right

UNIT – IV
1. What are the two ways of representing binary trees in the memory? Which one do you prefer
and why?
2. List all possible non-similar binary trees havingfour nodes.
3. Draw the binary expression tree that representsthe following postfix expression:
AB+C*D–
4. Consider the tree given below. Now, do thefollowing:
(a) Name the leaf nodes (b) Name the non-leaf nodes
(c) Name the ancestors of E (d) Name the descendants of A
(e) Name the siblings of C (f) Find the height of the tree
(g) Find the height of sub-tree rooted at E (h) Find the level of node E
(i) Find the in-order, pre-order, post-order, and levelordertraversal

5. For the expression tree given below, do thefollowing:


(a) Extract the infix expression it represents
(b) Find the corresponding prefix and postfixexpressions
(c) Evaluate the infix expression, given a = 30, b =10, c = 2, d = 30, e = 10
5. Consider the trees given below and state whetherthey are complete binary tree or full binary tree.

7. What is the maximum number of levels that abinary search tree with 100 nodes can have?
11. What is the maximum height of a tree with 32nodes?
12. What is the maximum number of nodes that canbe found in a binary tree at levels 3, 4, and 12?
13. Draw all possible non-similar binary trees havingthree nodes.
Binary search trees
Consider the binary search tree given below. Now do the following operations:
a. Find the result of in-order, pre-order, and post-order traversals.
b. Show the deletion of the root node
c. Insert 11, 22, 33, 44, 55, 66, and 77 in the tree
Consider the AVL tree given below and insert 18,81, 29, 15, 19, 25, 26, and 1 in it.
Delete nodes 39, 63, 15, and 1 from the AVL treeformed after solving the above question.

4. Differentiate between a min-heap and a max-heap.


4. Explain the steps involved in inserting a new value in a binary heap with the help of a suitable example.
5. Explain the steps involved in deleting a value from a binary heap with the help of a suitable example.
6. Discuss the applications of binary heaps.
7. Form a binary max-heap and a min-heap from the following sequence of data:
8. 50, 40, 35, 25, 20, 27, 33.
9. Heaps are excellent data structures to implement priority queues. Justify this statement.
10. Define a binomial heap. Draw its structure.
11. Differentiate among binary, binomial, andFibonacci heaps.
12. Explain the operations performed on a Fibonacci heap.
13. Why are Fibonacci heaps preferred over binary and binomial heaps?
14. Analyse the complexity of the algorithm to unite two binomial heaps.
15. The running time of the algorithm to find the minimum key in a binomial heap is O(log n).Comment.
UNIT – V
1. Explain the relationship between a linked liststructure and a digraph.
2. What is a graph? Explain its key terms.
3. How are graphs represented inside a computer’smemory? Which method do you prefer and why?
4. Consider the graph given below.
(a) Write the adjacency matrix of G.
(b) Write the path matrix of G.
(c) Is the graph biconnected?
(d) Is the graph complete?
(e) Find the shortest path matrix using Warshall’salgorithm.
5. Explain the graph traversal algorithms in detailwith example.
6. Draw a complete undirected graph having fivenodes.
7. Consider the graph given below and find out thedegree of each node.

8. Consider the graph given below. State all thesimple paths from A to D, B to D, and C to D.
Also, find out the in-degree and out-degree of eachnode. Is there any source or sink in the graph?

9. Consider the graph given below. Find out itsdepth-first and breadth-first traversal scheme.

10. Differentiate between depth-first search andbreadth-first search traversal of a graph.


11. Explain the topological sorting of a graph G.
12. Define spanning tree.
13. When is a spanning tree called a minimumspanning tree? Take a weighted graph of your
choice and find out its minimum spanning tree.
14. Explain Prim’s algorithm.
15. Write a brief note on Kruskal’s algorithm.
16. Write a short note on Dijkstra’s algorithm

You might also like