QUEUE OPERATION USING LINKED LIST

Previous Next


Program


#include <>
#include <>
#define NULL 0
struct node
{
int data;
struct node *link;
};
struct node *front=NULL,*rear=NULL,*newnode,*temp;
void main()
{
int ch;
clrscr();
printf("\n\t\tQUEUE OPERATION USING LINKED LIST");
printf("\n\t\t--------------------------------------------------------- ");
do
{
printf("\n 1.INSERT\n 2.DELETE\n 3.DISPLAY\n 4.EXIT");
printf("\n Enter Your Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
}
}while(ch!=4);
getch();
}
enqueue()
{
newnode=(struct node*)malloc(sizeof (struct node));
printf("\n Enter The Data :");
scanf("%d",&newnode- >data);
newnode- >link=NULL;
printf("\n The inserted data is:%d",newnode- >data);
if(front==NULL)
front=newnode;
else
rear- >link=newnode;
rear=newnode;
}
dequeue()
{
temp=front;
if(front==NULL)
printf("\n Queue is empty");
else
{
printf("\n The deleted data is:%d",temp- >data);
front=front- >link;
}
free(temp);
}
display()
{
temp=front;
if(front==NULL)
printf("\n Queue is empty");
else
{
printf("\n Queue Values are:");
while(temp!=NULL)
{
printf("\n%d",temp- >data);
temp=temp- >link;
}
}
}


Output

Enter the queue size: 3

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 1

Enter The Data : 5
The inserted data is: 5

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 1

Enter The Data : 10
The inserted data is: 10

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 3

Queue Values are:
5
10

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 2

The deleted data is: 5

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 3

Queue Values are:
10

1.INSERT
2.DELETE
3.DISPLAY
4.EXIT
Enter Your Choice: 4