Queues using Linked list
It configures storage (using arrays), queues where harder to manipulate then were stacks.
It causes difficulties to handle full queues and empty queues. It is for queues that linked storage really comes into its own
The linked implementation has two advantages over the array implementation (1) it is faster locations for insertion and deletion are same at the back and at the front and 2) it wastes no space
Linked queues are just as easy to handle as are linked stacks.
Program:
Program to implement queue using linked list :
# include<stdio.h>
#include<conio.h>
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL ;*rear;
void insertion()
{
int els;
struct node *new node;
new node=(struct node *)malloc(sizeof(struct node));
printf(“Enter data for new node: ”);
scanf(“%d”,&ele);
newnode data=ele;
newnode next=NULL;
if(front==NULL)
{
front=new node;
rear=front;
}
else
{
rear next= new code;
rear= new node;
}
}
void deletion()
{
struct node *temp;
if(front==NULL)
printf(“QUEUE underflow”);
else
{
temp=front;
front=front next;
printf(“Deleted element is : %d \n”, temp data);
free(temp);
}
}
void display()
{
struct node *ptr;
if(front===NULL)
printf(“queue is empty”);
else
{
printf(“elements of queue are :”);
for(ptr=front; ptr!=NULL;ptr=ptrnext);
printf(“%5d”,ptr data);
}
}
void main()
{
int ch;
clrscr();
do
{
printf(“1. INSERTION \n”);
printf(“2. DELETION \n”);
printf(“3. DISPLAY \n”);
printf(“4.EXIT \n”);
printf(“Enter U r choice :”);
scanf(“%d”,&ch);
switch(ch)
{
case1: insertion(); break;
case2: deletion(); break;
case3: display(); break;
case 4: exit(0);
}
} while(ch<=4)
getch();
}