0% found this document useful (0 votes)
17 views59 pages

Ds Lab Manual

The Data Structures Lab manual outlines the course objectives, experiments, and assessment methods for students to develop skills in designing and analyzing data structures. It includes a list of programming tasks in C, such as creating and manipulating linked lists, stacks, queues, and implementing various sorting algorithms. The course aims to enhance practical knowledge and the ability to apply suitable data structures to real-world problems.

Uploaded by

chakrapriya7
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)
17 views59 pages

Ds Lab Manual

The Data Structures Lab manual outlines the course objectives, experiments, and assessment methods for students to develop skills in designing and analyzing data structures. It includes a list of programming tasks in C, such as creating and manipulating linked lists, stacks, queues, and implementing various sorting algorithms. The course aims to enhance practical knowledge and the ability to apply suitable data structures to real-world problems.

Uploaded by

chakrapriya7
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/ 59

DATA STRUCTURES LAB

MANUAL

DATA STRUCTURES LAB SYLLABUS

Course Code Course Name Course Category L-T-P Credits


20CS1282 Data PCC 0-0-3 1.5
Structures Lab

Course Objectives:
1. To develop skills to design and analyze simple linear and non-linear data
structures
2. To strengthen the ability to identify and apply the suitable data structures for
the given real-
world problem
3. To gain knowledge in practical applications of data structures.

List of Experiments:
1. Write a C program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.
2. Write a C program that uses functions to perform the following:
a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.
3. Write a C program implement the Stack ADT using Arrays and Linked List.
4. Write a C program that uses stack operations to convert a given infix
expression into its postfix equivalent.
5. Write a C program that evaluates a postfix expression.
6. Write C program to implement queue ADT using array and doubly linked list.
7. a) Write C program to implement priority queue ADT using array.
b) Write C program to implement circular queue ADT using array.
8. Write C program for implementing the following sorting methods:
a) Insertion sort
b) Merge sort
9. Write C program for implementing the following sorting methods:
a) Quick sort
b) Selection sort
10. Write a C program for implementing Heap sort algorithm.
11. Write a C program that uses functions to perform the following:
a) Create a Binary Search Tree (BST).
b) Insert data in BST
c) Traverse the above BST recursively in Postorder.
12. Write a C program that uses functions to perform the following:
a) Deletion an element BST
b) Traverse the above BST non recursively in Inorder.
13. Write a C program to implement all the functions of a dictionary (ADT)
using hashing.
14. Write C program for implementing Depth first traversal and Breadth first
traversal.

Course Outcomes:
At the end of this lab session, the student will

CO 1 Be able to design and analyze the time and space efficiency of the
data structure
CO 2 Be capable to identity the appropriate data structure for given
problem
CO 3 Have practical knowledge on the application of data structures

Assessment Method:
Assessment Tool Experiments Report/Viva-Voce/ Total
Quiz/MCQ/Lab
project
Weightage (%) 25% 15% 40%

Course Nature Practical


Assessment Method
Assessment Tool Experiments Record Report/Viva-Voce/ Total
Quiz/MCQ/Lab
projectTotal
Weightage (%) 25% 5% 10% 40%
End Semester Examination weightage (%) 60%
PROGRAMS
1. Write a C program that uses functions to perform the following:
a) Create a singly linked list of integers.
b) Delete a given integer from the above linked list.
c) Display the contents of the above list after deletion.

PROGRAM:

//SINGLY LINKED LIST


#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *head=NULL,*newnode,*temp,*temp1;
struct node *create(struct node *head){
int option;
while(option){
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter the value in data field : ");
scanf("%d",&newnode->data);
newnode->next=NULL;
if(head==NULL){
head=temp=newnode;
printf("\nOne node created!\n");
}
else{
temp->next=newnode;
temp=newnode;
}
printf("Do you want to continue creating nodes to be in the singly linked
list ? \nPress any number to continue creating nodes or 0 to terminate creating
nodes : ");
scanf("%d",&option);
}
printf("\n");
printf("Singly Linked List is created...!\n");
return head;
}
struct node *display(struct node *head){
if(head==NULL) printf("List is empty..\nNothing to display..\n");
else{
temp=head;
printf("The nodes present in the singly linked list : \n");
while(temp!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
}
return head;
}
struct node *insbeg(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
newnode->next=head;
head=newnode;
printf("Newnode having data %d inserted at the beginning of the existing
linked list.\n",num);
}
return head;
}
struct node *insend(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->next!=NULL) temp=temp->next;
temp->next=newnode;
newnode->next=NULL;
printf("Newnode having data %d inserted at the ending of the existing linked
list.\n",num);
}
return head;
}
struct node *insbeforenode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted before to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->next=temp;
printf("Newnode having data %d inserted before to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *insafternode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted after to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head,temp1=head;
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->next=temp;
printf("Newnode having data %d inserted after to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *delbeg(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head;
head=head->next;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the beginning of the list\n");
}
return head;
}
struct node *delend(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head;
while(temp->next!=NULL){
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the end of the list\n");
}
return head;
}
struct node *delnode(struct node *head){
int value;
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
printf("Enter the data of the node to be deleted in the given list : ");
scanf("%d",&value);
temp=head;
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("Given node is deleted from the list\n");
}
return head;
}
struct node *delafternode(struct node *head){
int value;
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
printf("Enter the data of the node to be deleted after to the given node in the
given list : ");
scanf("%d",&value);
temp=head,temp1=head;
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted after to the given node from the list\n");
}
return head;
}
struct node *dellist(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head;
while(temp->next!=NULL){
free(temp);
temp=temp->next;
}
printf("Entire Singly Linked List is deleted...!\n");
}
return head;
}
void main()
{
int option;
while(option){
printf("\n1. Creating a Singly Linked List\n2. Displaying the list\n3. Insertion of
newnode at beginning of the list\n4. Insertion of newnode at ending of the list\
n5. Insertion of newnode before to the given node in the list\n6. Insertion of
newnode after to the given node in the list\n7. Deletion of node from beginning
of the list\n8. Deletion of node from the end of the list\n9. Deletion of given
node from the list\n10. Deletion of node after to the given node from the list\
n11. Deletion of entire list\n");
printf("\nEnter an option of your choice : ");
scanf("%d",&option);
switch(option){
case 1: printf("\n***** Creating a Singly Linked List *****\n");
head = create(head);
break;
case 2: printf("\n***** Displaying the list *****\n");
head = display(head);
break;
case 3: printf("\n***** Insertion of newnode at beginning of the list *****\n");
head = insbeg(head);
break;
case 4: printf("\n***** Insertion of newnode at ending of the list *****\n");
head = insend(head);
break;
case 5: printf("\n***** Insertion of newnode before to the given node in the list
*****\n");
head = insbeforenode(head);
break;
case 6: printf("\n***** Insertion of newnode after to the given node in the list
*****\n");
head = insafternode(head);
break;
case 7: printf("\n***** Deletion of node from beginning of the list *****\n");
head = delbeg(head);
break;
case 8: printf("\n***** Deletion of node from the end of the list *****\n");
head = delend(head);
break;
case 9: printf("\n***** Deletion of given node from the list *****\n");
head = delnode(head);
break;
case 10: printf("\n***** Deletion of node after to the given node from the list
*****\n");
head = delafternode(head);
break;
case 11: printf("\n***** Deletion of entire list *****\n");
head = dellist(head);
break;
default: printf("Please enter a valid option.....\n");
break;
}
}
}

OUTPUT:

1. Creating a Singly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Deletion of node from beginning of the list
8. Deletion of node from the end of the list
9. Deletion of given node from the list
10. Deletion of node after to the given node from the list
11. Deletion of entire list
Enter an option of your choice : 1

***** Creating a Singly Linked List *****


Enter the value in data field : 2

One node created!


Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 4
Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 6
Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 8
Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 10
Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 12
Do you want to continue creating nodes to be in the singly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
0

Singly Linked List is created...!

1. Creating a Singly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Deletion of node from beginning of the list
8. Deletion of node from the end of the list
9. Deletion of given node from the list
10. Deletion of node after to the given node from the list
11. Deletion of entire list

Enter an option of your choice : 2

***** Displaying the list *****


The nodes present in the singly linked list :
2 4 6 8 10 12
1. Creating a Singly Linked List
2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Deletion of node from beginning of the list
8. Deletion of node from the end of the list
9. Deletion of given node from the list
10. Deletion of node after to the given node from the list
11. Deletion of entire list

Enter an option of your choice : 9

***** Deletion of given node from the list *****


Enter the data of the node to be deleted in the given list : 6
The deleted node having data is 6
Given node is deleted from the list

1. Creating a Singly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Deletion of node from beginning of the list
8. Deletion of node from the end of the list
9. Deletion of given node from the list
10. Deletion of node after to the given node from the list
11. Deletion of entire list

Enter an option of your choice : 2

***** Displaying the list *****


The nodes present in the singly linked list :
2 4 8 10 12
1. Creating a Singly Linked List
2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Deletion of node from beginning of the list
8. Deletion of node from the end of the list
9. Deletion of given node from the list
10. Deletion of node after to the given node from the list
11. Deletion of entire list

Enter an option of your choice : 0


Please enter a valid option.....

2. Write a C program that uses functions to perform the following:


a) Create a doubly linked list of integers.
b) Delete a given integer from the above doubly linked list.
c) Display the contents of the above list after deletion.

PROGRAM:

//DOUBLY LINKED LIST

#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL,*newnode,*temp,*temp1;
struct node *create(struct node *head){
int option;
while(option){
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter the value in data field : ");
scanf("%d",&newnode->data);
newnode->next=NULL;
newnode->prev=NULL;
if(head==NULL){
head=temp=newnode;
printf("\nOne node created!\n");
}
else{
temp->next=newnode;
newnode->prev=temp;
temp=newnode;
}
printf("Do you want to continue creating nodes to be in the doubly linked list ?
\nPress any number to continue creating nodes or 0 to terminate creating
nodes : ");
scanf("%d",&option);
}
printf("\n");
printf("Doubly Linked List is created...!\n");
return head;
}
struct node *display(struct node *head){
if(head==NULL) printf("List is empty..\nNothing to display..\n");
else{
temp=head;
printf("The nodes present in the Doubly Linked list : \n");
while(temp->next!=NULL){
printf("%d\t",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
return head;
}
struct node *insbeg(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
newnode->prev=NULL;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
newnode->next=head;
head->prev=newnode;
head=newnode;
printf("Newnode having data %d inserted at the beginning of the existing
linked list.\n",num);
}
return head;
}
struct node *insend(struct node *head){
int num;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
newnode->next=NULL;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->next!=NULL) temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
newnode->next=NULL;
printf("Newnode having data %d inserted at the ending of the existing linked
list.\n",num);
}
return head;
}
struct node *insbeforenode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted before to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
head=newnode;
newnode->next=NULL;
newnode->prev=NULL;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->prev=temp1;
newnode->next=temp;
temp->prev=newnode;
printf("Newnode having data %d inserted before to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *insafternode(struct node *head){
int num,value;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the value present in the list for which the newnode has to be
inserted after to that given node : ");
scanf("%d",&value);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
newnode->prev=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head,temp1=head;
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=newnode;
newnode->prev=temp1;
newnode->next=temp;
temp->prev=newnode;
printf("Newnode having data %d inserted after to the given node having
data %d in the existing linked list.\n",num,value);
}
return head;
}
struct node *inspos(struct node *head){
int num,pos,i=1;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the position in which the newnode has to be inserted : ");
scanf("%d",&pos);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(i<pos-1){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
printf("Newnode having data %d inserted at the given position %d in the
existing linked list.\n",num,pos);
}
return head;
}
struct node *insposafter(struct node *head){
int num,pos,i=1;
printf("Enter the value to be filled in the data part of the newnode : ");
scanf("%d",&num);
printf("Enter the position in which the newnode has to be inserted after that
position : ");
scanf("%d",&pos);
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=num;
if(head==NULL){
newnode->next=NULL;
head=newnode;
printf("Newnode having data %d inserted to the list\n",num);
}
else{
temp=head;
while(i<pos){
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
printf("Newnode having data %d inserted after to the given position %d in
the existing linked list.\n",num,pos);
}
return head;
}
struct node *delbeg(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head;
head=head->next;
head->prev=NULL;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the beginning of the list\n");
}
return head;
}
struct node *delend(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
temp=head,temp1=head;
while(temp->next!=NULL){
temp1=temp;
temp=temp->next;
}
temp->prev=temp1;
temp1->next=NULL;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted from the ending of the list\n");
}
return head;
}
struct node *delnode(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
int value;
temp=head,temp1=head;
printf("Enter the data of the node to be deleted in the given list : ");
scanf("%d",&value);
while(temp->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp->prev;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("Given node is deleted from the list\n");
}
return head;
}
struct node *delafternode(struct node *head){
if(head==NULL) printf("List is empty..!\nDeletion is not possible..!\n*****
UNDERFLOW *****\n");
else{
int value;
temp=head,temp1=head;
printf("Enter the data of the node to be deleted after to the given node in the
given list : ");
scanf("%d",&value);
while(temp1->data!=value){
temp1=temp;
temp=temp->next;
}
temp1->next=temp->next;
temp->next->prev=temp->prev;
printf("The deleted node having data is %d\n",temp->data);
free(temp);
printf("One node is deleted after to the given node from the list\n");
}
return head;
}
void main()
{
int option;
while(option){
printf("\n1. Creating a Doubly Linked List\n2. Displaying the list\n3. Insertion of
newnode at beginning of the list\n4. Insertion of newnode at ending of the list\
n5. Insertion of newnode before to the given node in the list\n6. Insertion of
newnode after to the given node in the list\n7. Insertion of newnode at given
position\n8. Insertion of newnode after the given position\n9. Deletion of node
from beginning of the list\n10. Deletion of node from the end of the list\n11.
Deletion of given node from the list\n12. Deletion of node after to the given
node from the list\n");
printf("\nEnter an option of your choice : ");
scanf("%d",&option);
switch(option){
case 1: printf("\n***** Creating a Doubly Linked List *****\n");
head = create(head);
break;
case 2: printf("\n***** Displaying the list *****\n");
head = display(head);
break;
case 3: printf("\n***** Insertion of newnode at beginning of the list *****\n");
head = insbeg(head);
break;
case 4: printf("\n***** Insertion of newnode at ending of the list *****\n");
head = insend(head);
break;
case 5: printf("\n***** Insertion of newnode before to the given node in the list
*****\n");
head = insbeforenode(head);
break;
case 6: printf("\n***** Insertion of newnode after to the given node in the list
*****\n");
head = insafternode(head);
break;
case 7: printf("\n***** Insertion of newnode at given position *****\n");
head = inspos(head);
break;
case 8: printf("\n***** Insertion of newnode after the given position *****\n");
head = insposafter(head);
break;
case 9: printf("\n***** Deletion of node from beginning of the list *****\n");
head = delbeg(head);
break;
case 10: printf("\n***** Deletion of node from the end of the list *****\n");
head = delend(head);
break;
case 11: printf("\n***** Deletion of given node from the list *****\n");
head = delnode(head);
break;
case 12: printf("\n***** Deletion of node after to the given node from the list
*****\n");
head = delafternode(head);
break;
default: printf("Please enter a valid option.....\n");
break;
}
}
}

OUTPUT:

1. Creating a Doubly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Insertion of newnode at given position
8. Insertion of newnode after the given position
9. Deletion of node from beginning of the list
10. Deletion of node from the end of the list
11. Deletion of given node from the list
12. Deletion of node after to the given node from the list
Enter an option of your choice : 1

***** Creating a Doubly Linked List *****


Enter the value in data field : 2

One node created!


Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 4
Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 6
Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 8
Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 10
Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
1
Enter the value in data field : 12
Do you want to continue creating nodes to be in the doubly linked list ?
Press any number to continue creating nodes or 0 to terminate creating nodes :
0

Doubly Linked List is created...!

1. Creating a Doubly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Insertion of newnode at given position
8. Insertion of newnode after the given position
9. Deletion of node from beginning of the list
10. Deletion of node from the end of the list
11. Deletion of given node from the list
12. Deletion of node after to the given node from the list

Enter an option of your choice : 2

***** Displaying the list *****


The nodes present in the Doubly Linked list :
2 4 6 8 10 12
1. Creating a Doubly Linked List
2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Insertion of newnode at given position
8. Insertion of newnode after the given position
9. Deletion of node from beginning of the list
10. Deletion of node from the end of the list
11. Deletion of given node from the list
12. Deletion of node after to the given node from the list

Enter an option of your choice : 11

***** Deletion of given node from the list *****


Enter the data of the node to be deleted in the given list : 8
The deleted node having data is 8
Given node is deleted from the list

1. Creating a Doubly Linked List


2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Insertion of newnode at given position
8. Insertion of newnode after the given position
9. Deletion of node from beginning of the list
10. Deletion of node from the end of the list
11. Deletion of given node from the list
12. Deletion of node after to the given node from the list

Enter an option of your choice : 2

***** Displaying the list *****


The nodes present in the Doubly Linked list :
2 4 6 10 12
1. Creating a Doubly Linked List
2. Displaying the list
3. Insertion of newnode at beginning of the list
4. Insertion of newnode at ending of the list
5. Insertion of newnode before to the given node in the list
6. Insertion of newnode after to the given node in the list
7. Insertion of newnode at given position
8. Insertion of newnode after the given position
9. Deletion of node from beginning of the list
10. Deletion of node from the end of the list
11. Deletion of given node from the list
12. Deletion of node after to the given node from the list

Enter an option of your choice : 0


Please enter a valid option.....
3. Write a C program implement the Stack ADT using Arrays and
Linked List.

PROGRAM:

//STACK IMPLEMENTATION USING ARRAYS


#include<stdio.h>
#define n 5
int stack[n];
int top=-1;
int push();
int pop();
int peek();
int display();
int main()
{
int option;
do
{
printf("\n***********************MENU**********************");
printf("\n1.Push\n2.Pop\n3.peek\n4.Display");
printf("\n****************************************************");
printf("\nEnter one of the above options to perform.....:");
scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
default: printf("\nInvalid Option please enter correct position");
}
}while(option);
}

int push()
{
int val;
printf("Enter the val to push in to the stack..:");
scanf("%d",&val);
if(top>=n-1) printf("\nOverFlow.The stack is full.");
else
{
top++;
stack[top]=val;
}
}
int pop()
{
int temp;
if(top<=-1) printf("\nUnderFlow.The stack is Empty.");
else
{
temp=stack[top];
top--;
printf("\nThe poped item is..:%d",temp);
}
}

int peek()
{
if(top<=-1) printf("\nUnderFlow.The stack is Empty.");
else
{
printf("\nThe peek item is..:%d",stack[top]);
}
}

int display()
{
int i;
for(i=top;i>=0;i--)
{
printf("\t%d",stack[i]);
}
}

OUTPUT:
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:2

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:4

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:6

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:8

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:10

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1
Enter the val to push in to the stack..:12

OverFlow.The stack is full.


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4
10 8 6 4 2
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:2

The poped item is..:10


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4
8 6 4 2
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:3

The peek item is..:8


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4
8 6 4 2
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:0

Invalid Option please enter correct position

PROGRAM:
//STACK IMPLEMENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
node *top=NULL;
int push();
int pop();
int peek();
int display();
int main()
{
int option;
do
{
printf("\n***********************MENU**********************");
printf("\n1.Push\n2.Pop\n3.peek\n4.Display");
printf("\n****************************************************");
printf("\nEnter one of the above options to perform.....:");
scanf("%d",&option);
switch(option)
{
case 1:push();
break;
case 2:pop();
break;
case 3:peek();
break;
case 4:display();
break;
default: printf("\nInvalid Option please enter correct position");
}
}while(option);
}

int push()
{
int x;
node *new_node;
printf("\nEnter new_node data...:");
scanf("%d",&x);
new_node=(node *) malloc(sizeof(node));
new_node->data=x;
new_node->next=top;
top=new_node;
}

int pop()
{
node *temp;
temp=top;
if(top==NULL) printf("\nStack is Empty");
else
{
printf("The popped item is...:%d",top->data);
top=top->next;
free(temp);
}
}

int peek()
{
if(top==NULL) printf("\nStack is Empty");
else
{
printf("The Peek item is...:%d",top->data);
}
}

int display()
{
node *temp;
temp=top;
if(top==NULL) printf("\nStack is Empty");
else
{
printf("\nThe stack Elements are...:");
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
}
}

OUTPUT:
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:2

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:4

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:6


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:8

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:10

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:1

Enter new_node data...:12

***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4

The stack Elements are...: 12 10 8 6 4 2


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:2
The popped item is...:12
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4

The stack Elements are...: 10 8 6 4 2


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:3
The Peek item is...:10
***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:4

The stack Elements are...: 10 8 6 4 2


***********************MENU**********************
1.Push
2.Pop
3.peek
4.Display
****************************************************
Enter one of the above options to perform.....:0

Invalid Option please enter correct position

4. Write a C program that uses stack operations to convert a given infix


expression into its postfix equivalent.

PROGRAM:
#include<stdio.h>
char stack[100];
int top=-1;
void push(char x){
stack[++top]=x;
}
char pop(){
if(top==-1) return -1;
return stack[top--];
}
int priority(char x){
if(x==’c’) return 0;
if(x==’+’ || x==’-’) return 1;
if(x==’*’ || x==’/’) return 2;
}
int main(){
char exp[100],*e,x;
printf(“Enter an expression: ”);
scanf(“%s”,exp);
printf(“\n”);
printf(“Postfix Expression: \n”);
e=exp;
while(*e!=’\0’){
if(isalnum(*e)) printf(“%c”,x);
else if(*e==’c’) push(*e);
else if(*e==’)’){
while(x==pop()!=’c’) printf(“%c”,x);
}
else{
while(priority(stack[top])>=priority(*e)) printf(“%c”,pop());
push(*e);
}
e++;
}
while(top!=-1) printf(“%c”,pop());
}

OUTPUT:

Enter an expression: a+b-c*d*(e+f-g)/h


Postfix Expression:
ab+cd*ef+g-*h/-

5. Write a C program that evaluates a 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 '/':
{
n3 = n2 / n1;
break;
}
}
push(n3);
}
e++;
}
printf("\nThe result of expression %s = %d\n\n",exp,pop());
return 0;
}

OUTPUT:

Enter the expression :: 245+*


The result of expression 245+* = 18

6. Write C program to implement queue ADT using array and doubly


linked list.
PROGRAM:
//QUEUE IMPLEMENTATION USING ARRAY
#include<stdio.h>
#define max 5
int Queue[max];
int front=-1,rear=-1;
int enqueue()
{
int x;
printf("\nEnter data to be Enqueue in to Queue...:");
scanf("%d",&x);
if(rear==max-1) printf("\nQueue is Full,OverFlow");
else if(front==-1 || rear==-1)
{
front=rear=0;
Queue[front]=x;
}
else
{
rear++;
Queue[rear]=x;
}
}

void Dequeue()
{
if(front==-1 || front>rear) printf("\nList is Empty..,UnderFlow");
else if(front==rear)
{
front=rear=0;
}
else
{
printf("\nDeleted data in Queue is...:%d",Queue[front]);
front=front+1;
}
}

void display()
{
int i;
if(front==-1 || rear==-1) printf("\nQueue is Empty");
else
{
printf("\nThe Queue Elements are...:");
for(i=front;i<=rear;i++) printf("\t%d",Queue[i]);
}
}

void peek()
{
if(front==-1 || rear==-1) printf("\nQueue is Empty");
else
{
printf("The front element in the Queue is..:%d",Queue[front]);
}
}

int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}

OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:2

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1
Enter data to be Enqueue in to Queue...:4

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:6

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:8

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:12

Queue is Full,OverFlow

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3

The Queue Elements are...: 2 4 6 8 10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2

Deleted data in Queue is...:2

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3

The Queue Elements are...: 4 6 8 10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4
The front element in the Queue is..:4

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3

The Queue Elements are...: 4 6 8 10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:0
Please Enter correct option

PROGRAM:
//QUEUE IMPLEMENTATION USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *front=0,*rear=0;
void enqueue()
{
struct node *newnode;
int x;
newnode=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the data in the queue...:");
scanf("%d",&x);
newnode->data=x;
newnode->next=NULL;
if(front==NULL && rear==NULL)
{
front=rear=newnode;
}
else
{
rear->next=newnode;
rear=newnode;
}
}

void Dequeue()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
temp=front;
printf("\nThe deleted Element is..:%d",temp->data);
front=front->next;
free(temp);
}
}

void display()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
temp=front;
printf("\nThe Queue Elements are..:");
while(temp!=0)
{
printf("\t%d",temp->data);
temp=temp->next;
}
}
}

void peek()
{
struct node *temp;
if(front==NULL || front>rear) printf("\nQueue is Empty");
else
{
printf("\nThe peek Element is..:%d",front->data);
}
}
int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}

OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter the data in the queue...:2


*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter the data in the queue...:3

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter the data in the queue...:4

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter the data in the queue...:5

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3

The Queue Elements are..: 2 3 4 5

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2
The deleted Element is..:2

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2

The deleted Element is..:3

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3

The Queue Elements are..: 4 5

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4

The peek Element is..:4

7. a) Write C program to implement priority queue ADT using array.


b) Write C program to implement circular queue ADT using array.

a)PROGRAM:

// Priority Queue implementation in C

#include <stdio.h>
int size = 0;
void swap(int *a, int *b) {
int temp = *b;
*b = *a;
*a = temp;
}

// Function to heapify the tree


void heapify(int array[], int size, int i) {
if (size == 1) {
printf("Single element in the heap");
} else {
// Find the largest among root, left child and right child
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && array[l] > array[largest])
largest = l;
if (r < size && array[r] > array[largest])
largest = r;

// Swap and continue heapifying if root is not largest


if (largest != i) {
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}

// Function to insert an element into the tree


void insert(int array[], int newNum) {
if (size == 0) {
array[0] = newNum;
size += 1;
} else {
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(array, size, i);
}
}
}

// Function to delete an element from the tree


void deleteRoot(int array[], int num) {
int i;
for (i = 0; i < size; i++) {
if (num == array[i])
break;
}

swap(&array[i], &array[size - 1]);


size -= 1;
for (int i = size / 2 - 1; i >= 0; i--) {
heapify(array, size, i);
}
}

// Print the array


void printArray(int array[], int size) {
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
}
int main() {
int array[10];

insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);

printf("Max-Heap array: ");


printArray(array, size);

deleteRoot(array, 4);

printf("After deleting an element: ");

printArray(array, size);
}

OUTPUT:
Max-Heap array: 9 5 4 3 2
After deleting an element: 9 5 2 3

b)PROGRAM:
#include<stdio.h>
#define max 5
int Queue[max];
int front=-1,rear=-1;
int enqueue()
{
int x;
if((rear==max-1 && front==0)|| (rear+1==front)) printf("\nOverFlow
Condition,Queue is Full");
else
{
printf("\nEnter data to be Enqueue in to Queue...:");
scanf("%d",&x);
if(front==-1 && rear==-1)
{
front=rear=0;
Queue[rear]=x;
}
else if(rear==max-1 && front!=0) rear=0;
else rear++;
Queue[rear]=x;
}
}

void Dequeue()
{
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else if(front==rear)
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front=rear=-1;
}
else if(front==max-1)
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front=0;
}
else
{
printf("\nThe Dequeued Element is..:%d",Queue[front]);
front++;
}
}

void display()
{
int i,j;
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else if(front>rear)
{
for(i=front;i<=max-1;i++) printf("\t%d",Queue[i]);
for(j=0;j<=rear;j++) printf("\t%d",Queue[j]);
}
else
{
for(i=front;i<=rear;i++) printf("\t%d",Queue[i]);
}
}

void peek()
{
if(front==-1 && rear==-1) printf("\nUnderFlow Condition,Queue is Empty");
else
{
printf("The peek of the Element in the queue is..:%d",Queue[front]);
}
}
int main()
{
int option;
do
{
printf("\n\n*********************QUEUE OPERATIONS*********************");
printf("\n1.Enqueue\n2.Dequeue\n3.Display\n4.peek");
printf("\n****************************************************************");
printf("\nEnter one of the above options to perform..:");
scanf("%d",&option);
switch(option)
{
case 1:enqueue();
break;
case 2:Dequeue();
break;
case 3:display();
break;
case 4:peek();
break;
default: printf("Please Enter correct option");
}
}while(option);
}

OUTPUT:
*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:2

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:4

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:6

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:8

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

Enter data to be Enqueue in to Queue...:10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:1

OverFlow Condition,Queue is Full

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
2 4 6 8 10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:2

The Dequeued Element is..:2

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:3
4 6 8 10

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:4
The peek of the Element in the queue is..:4

*********************QUEUE OPERATIONS*********************
1.Enqueue
2.Dequeue
3.Display
4.peek
****************************************************************
Enter one of the above options to perform..:0
Please Enter correct option

8. Write C program for implementing the following sorting methods:


a) Insertion sort
b) Merge sort

a)PROGRAM:

#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(j>=0 && a[j]>temp)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nThe sorted array elements using insertion sort..:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}

OUTPUT:
Enter the number of elements in the array...:5

Enter the array Elements..:


Enter a[0] element...:3

Enter a[1] element...:2

Enter a[2] element...:6

Enter a[3] element...:4

Enter a[4] element...:7

The sorted array elements using insertion sort....: 2 3 4 6 7

b)PROGRAM:

#include<stdio.h>
int Merge(int a[],int lb,int mid,int ub)
{
int b[10];
int i=lb;
int j=mid+1;
int k=lb;
while(i<=mid && j<=ub)
{
if(a[i]<=a[j])
{
b[k]=a[i];
i++;
}
else
{
b[k]=a[j];
j++;
}
k++;
}
if(i>mid)
{
while(j<=ub)
{
b[k]=a[j];
j++;
k++;
}
}
else
{
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
}
for(k=lb;k<=ub;k++)
{
a[k]=b[k];
}
//return b[10];
}

int Merge_sort(int a[],int lb,int ub)


{
int mid;
if(lb<ub)
{
mid=(lb+ub)/2;
Merge_sort(a,lb,mid);
Merge_sort(a,mid+1,ub);
Merge(a,lb,mid,ub);
}
}
void main()
{
int a[10],n,i;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
Merge_sort(a,0,n-1);
printf("\nThe sorted array elements using insertion sort..:");
for(int i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}

OUTPUT:
Enter the number of elements in the array...:9

Enter the array Elements..:


Enter a[0] element...:7

Enter a[1] element...:8

Enter a[2] element...:4

Enter a[3] element...:3

Enter a[4] element...:5

Enter a[5] element...:2

Enter a[6] element...:1

Enter a[7] element...:7

Enter a[8] element...:6

The sorted array elements using insertion sort..: 1 2 3 4 5


6 7 7 8

9. Write C program for implementing the following sorting methods:


a) Quick sort
b) Selection sort

a)PROGRAM:

#include<stdio.h>
int swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
int Paritition(int a[10],int lb,int ub)
{
int start,end;
int pivot=a[lb];
start=lb;
end=ub;
//int i=(start-1);
while(start<end)
{
while(a[start]<=pivot)
{
start++;
}
while(a[end]>pivot) end--;
if(start<end)
{
swap(&a[start],&a[end]);
}
}
swap(&a[lb],&a[end]);
return end;
/*
for(int j=start;j<=end-1;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[end]);
return (i+1);
*/
}

int Quicksort(int a[],int lb,int ub)


{
int res;
if(lb<ub)
{
res=Paritition(a,lb,ub);
Quicksort(a,lb,res-1);
Quicksort(a,res+1,ub);
}
}
void print_array(int a[],int size)
{
int i;
printf("\nThe sorted array elements using Quick sort..:");
for(i=0;i<size;i++)
{
printf("\t%d",a[i]);
}
}
int main()
{
int a[10],n,i;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
Quicksort(a,0,n-1);
print_array(a,n);
}

OUTPUT:
Enter the number of elements in the array...:6

Enter the array Elements..:


Enter a[0] element...:5

Enter a[1] element...:7

Enter a[2] element...:8

Enter a[3] element...:9

Enter a[4] element...:3

Enter a[5] element...:5

The sorted array elements using Quick sort..: 3 5 5 7 8 9

b)PROGRAM:

#include<stdio.h>
void main()
{
int a[10],n,i,j,temp,min;
printf("\nEnter the number of elements in the array...:");
scanf("%d",&n);
printf("\nEnter the array Elements..:");
for(i=0;i<n;i++)
{
printf("\nEnter a[%d] element...:",i);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
{
min=j;
}
}
if(min!=i)
{
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
}
printf("\nThe sorted array elements using Selection sort..:");
for(i=0;i<n;i++)
{
printf("\t%d",a[i]);
}
}

OUTPUT:
Enter the number of elements in the array...:7

Enter the array Elements..:


Enter a[0] element...:5

Enter a[1] element...:3

Enter a[2] element...:4

Enter a[3] element...:2

Enter a[4] element...:1

Enter a[5] element...:6

Enter a[6] element...:7

The sorted array elements using Selection sort..: 1 2 3 4 5


6 7

10. Write a C program for implementing Heap sort algorithm.

PROGRAM:

#include <stdio.h>

void swap(int* a, int* b)


{
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i)


{
int left, right, largest;
largest = i;
left = 2 * i + 1;
right = 2 * i + 2;

// Check if left child exists and is larger than its parent


if (left < n && arr[left] > arr[largest])
largest = left;
// Check if right child exists and larger than its parent
if (right < n && arr[right] > arr[largest])
largest = right;

// if root is not the largest


if (largest != i) {
swap(&arr[i], &arr[largest]); //make root the largest
heapify(arr, n, largest); // Apply heapify to the largest node
}
}

void heap_sort(int arr[], int n)


{
int i;
for (i = (n / 2) - 1; i >= 0; i--)
heapify(arr, n, i);

for (i = n - 1; i >= 0; i--) {


swap(&arr[0], &arr[i]); //Move the largest element at root to the end
heapify(arr, i, 0); //Apply heapify to reduced heap
}
}

int main()
{
int arr[] = { 20, 13, 34, 56, 12, 10 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("Array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);

heap_sort(arr, n);

printf("\nAfter performing Heap Sort:\n");


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

return 0;
}

OUTPUT:

Array:
20 13 34 56 12 10
After performing Heap Sort:
10 12 13 20 34 56

11. Write a C program that uses functions to perform the following:


a) Create a Binary Search Tree (BST).
b) Insert data in BST
c) Traverse the above BST recursively in Postorder.

And

12. Write a C program that uses functions to perform the following:


a) Deletion an element BST
b) Traverse the above BST non recursively in Inorder.

PROGRAM:

#include <stdio.h>
#include <stdlib.h>

struct node {
int key;
struct node *left, *right;
};

// Create a node
struct node *newNode(int item) {
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

// Inorder Traversal
void inorder(struct node *root) {
if (root != NULL) {
// Traverse left
inorder(root->left);

// Traverse root
printf("%d -> ", root->key);

// Traverse right
inorder(root->right);
}
}

// Insert a node
struct node *insert(struct node *node, int key) {
// Return a new node if the tree is empty
if (node == NULL) return newNode(key);

// Traverse to the right place and insert the node


if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}

// Find the inorder successor


struct node *minValueNode(struct node *node) {
struct node *current = node;

// Find the leftmost leaf


while (current && current->left != NULL)
current = current->left;

return current;
}

// Deleting a node
struct node *deleteNode(struct node *root, int key) {
// Return if the tree is empty
if (root == NULL) return root;

// Find the node to be deleted


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

else {
// If the node is with only one child or no child
if (root->left == NULL) {
struct node *temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct node *temp = root->left;
free(root);
return temp;
}

// If the node has two children


struct node *temp = minValueNode(root->right);

// Place the inorder successor in position of the node to be deleted


root->key = temp->key;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->key);
}
return root;
}

// Driver code
int main() {
struct node *root = NULL;
root = insert(root, 8);
root = insert(root, 3);
root = insert(root, 1);
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 10);
root = insert(root, 14);
root = insert(root, 4);

printf("Inorder traversal: ");


inorder(root);

printf("\nAfter deleting 10\n");


root = deleteNode(root, 10);
printf("Inorder traversal: ");
inorder(root);
}

OUTPUT:
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 10 -> 14 ->
After deleting 10
Inorder traversal: 1 -> 3 -> 4 -> 6 -> 7 -> 8 -> 14 ->

13. Write a C program to implement all the functions of a dictionary


(ADT) using hashing.

PROGRAM:
#include <stdio.h>
#include <conio.h>
int ht[10], i, found = 0, key;
void insert_val();
void search_val();
void delete_val();
void display();
int main()
{
int option;
clrscr();
for ( i = 0;i < 10;i++ ) //to initialize every element as ‘–1’

ht[i] = –1;
do
{
printf( "\n MENU \n1.Insert \n2.Search \n3.Delete \n4.Display \n5.Exit");

printf( "\n Enter your option.");

scanf( "%d", &option);


switch (option)
{
case 1:
insert_val();
break;

case 2:
search_val();
break;

case 3:
delete_val();
break;

case 4:
display();
break;
default:

printf( "\nInvalid choice entry!!!\n" );


break;
}

}while (option!=5);
getch();

return 0;
}
void insert_val()
{

int val, f = 0;

printf( "\nEnter the element to be inserted : " );

scanf( "%d", &val );

key = ( val % 10 ) – 1;

if ( ht[key] == –1 )
{
ht[key] = val;
}
else
{

if ( key < 9 )
{
for ( i = key + 1;i < 10;i++ )

if ( ht[i] == –1 )
{
ht[i] = val;
break;
}

}
}

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


{
if ( ht[i] == –1 )

{
ht[i] = val;
break;
}
}
}
}
void display()
{
for (i = 0;i < 10;i++)
printf( "\t%d", ht[ i ] );
}
void search_val()
{
int val, flag = 0;
printf( "\nEnter the element to be searched :: " );
scanf( "%d", &val );
key = ( val % 10 ) – 1;
if ( ht[ key ] == val )

flag = 1;
else
{

for (i = key + 1;i < 10;i++)


{

if(ht[i] == val)
{
flag = 1;
key = i;
break;
}
}
}
if (flag == 0)
{

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


{
if (ht[ i ] == val)
{
flag = 1;

key = i;

break;
}
}
}
if (flag == 1)
{

found=1;

printf("\n The item searched was found at position %d !", key + 1 );


}
else
{

key = –1;

printf( "\nThe item searched was not found in the hash table" );
}
}
void delete_val()
{
search_val();
if (found==1)
{

if ( key != –1 )
{

printf( "\nThe element deleted is %d ", ht[ key ] );

ht[ key ] = –1;


}
}
}

OUTPUT:
MENU
1. Insert
2. Search
3. Delete
4. Display
5. Exit
Enter your option: 1
Enter the element to be inserted :1
Enter your option: 4
1 –1 –1 –1 –1 –1 –1 –1 –1 –1
Enter your option: 5

14. Write C program for implementing Depth first traversal and


Breadth first traversal.

PROGRAM:
//BREADTH FIRST SEARCH
#include <stdio.h>
#define MAX 10
void breadth_first_search(int adj[][MAX],int visited[],int start)
{
int queue[MAX],rear = –1,front =– 1, i;
queue[++rear] = start;
visited[start] = 1;
while(rear != front)
{

start = queue[++front];

if(start == 4)
printf("5\t");
else

printf("%c \t",start + 65);


for(i = 0; i < MAX; i++)
{
if(adj[start][i] == 1 && visited[i] == 0)
{
queue[++rear] = i;
visited[i] = 1;
}
}
}
}
int main()
{
int visited[MAX] = {0};
int adj[MAX][MAX], i, j;
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
breadth_first_search(adj,visited,0);
return 0;
}

OUTPUT:

Enter the adjaceny matrix:


01010
10110
01001
11001
00101
ABDCE

PROGRAM
//DEPTH FIRST SEARCH
#include <stdio.h>
#define MAX 5
void depth_first_search(int adj[][MAX],int visited[],int start)
{
int stack[MAX];
int top = –1, i;
printf("%c–",start + 65);
visited[start] = 1;
stack[++top] = start;
while(top ! = –1)
{

start = stack[top];
for(i = 0; i < MAX; i++)
{

if(adj[start][i] && visited[i] == 0)


{
stack[++top] = i;
printf("%c–", i + 65);
visited[i] = 1;
break;
}
}
if(i == MAX)
top––;
}
}
int main()
{
int adj[MAX][MAX];
int visited[MAX] = {0}, i, j;
printf("\n Enter the adjacency matrix: ");
for(i = 0; i < MAX; i++)
for(j = 0; j < MAX; j++)
scanf("%d", &adj[i][j]);
printf("DFS Traversal: ");
depth_first_search(adj,visited,0);
printf("\n");
return 0;
}

OUTPUT:
Enter the adjacency matrix:
01010
10110
01001
11001
00110
DFS Traversal: A –> C –> E –>

**********************************

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy