C program to create a queue using linked list with insert, delete and display elements in queue functions
PROGRAM:
#include<stdio.h>#include<stdlib.h>
struct node
{
int data;
struct node *link;
}*first=NULL;
void insert();
void delet();
void disp();
int main()
{
int choice;
while(1)
{
printf("\nEnter your choice\n");
printf("1 to insert element into queue\n");
printf("2 to delete element from the queue\n");
printf("3 to display elements in the queue\n");
printf("0 to exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2:delet();
break;
case 3:disp();
break;
case 0: return 0;
break;
default:printf("Invalid Entry\n");
}
}
}
void insert()
{
struct node *new_node,*temp;
new_node=(struct node *)malloc(sizeof(struct node) );
printf("Enter an element\n");
scanf("%d",&new_node->data);
new_node->link=NULL;
if(first==NULL)
{
first=new_node;
}
else
{
temp=first;
while(temp->link!=NULL)
{
temp=temp->link;
}
temp->link=new_node;
}
}
void delet()
{
struct node *x;
if(first==NULL)
printf("Delete not possible, Queue empty!\n");
else if(first->link==NULL)
{
printf("Element deleted is %d\n",first->data);
free(first);
first=NULL;
}
else
{
printf("Element deleted is %d\n",first->data);
x=first;
first=first->link;
free(x);
}
}
void disp()
{
struct node *temp;
if(first==NULL)
{
printf("Display not possible, Queue empty!\n");
}
else if(first->link==NULL)
{
printf("\nDisplaying elements in Queue\n");
printf("%d ",first->data);
}
else
{
temp=first;
printf("\nDisplaying elements in Queue\n");
while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->link;
}
printf("\n");
}
}
OUTPUT:
Enter your choice1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
4
Invalid Entry
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Display not possible, Queue empty!
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Delete not possible, Queue empty!
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
1
Enter an element
10
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Displaying elements in Queue
10
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
1
Enter an element
20
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
1
Enter an element
30
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
1
Enter an element
40
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Displaying elements in Queue
10 20 30 40
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Element deleted is 10
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Displaying elements in Queue
20 30 40
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Element deleted is 20
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Displaying elements in Queue
30 40
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Element deleted is 30
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Element deleted is 40
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
2
Delete not possible, Queue empty!
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
3
Display not possible, Queue empty!
Enter your choice
1 to insert element into queue
2 to delete element from the queue
3 to display elements in the queue
0 to exit
0