//Implement queue using linked list
#include<stdio.h>
#include<stdlib.h>
void insert();
void del();
void display();
struct node
{
int info;
struct node *link;
};
struct node *front,*rear;
void main()
{
int s;
while(1)
{
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter Your Choice:");
scanf("%d",&s);
switch(s)
{
case 1: insert();
break;
case 2: del();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nEnter correct value");
}
}
}
void insert()
{
struct node *ptr;
ptr=(struct node *)malloc(sizeof(struct node));
printf("\nEnter the node value");
scanf("%d",&ptr->info);
ptr->link=NULL;
if(front==NULL)
front=ptr;
else
rear->link=ptr;
rear=ptr;
}
void display()
{
struct node *ptr;
ptr=front;
if(front==NULL)
printf("\nQueue Empty");
else
{
while(ptr!=NULL)
{
printf("The Elements are:%d\n",ptr->info);
ptr=ptr->link;
}
}
}
void del()
{
struct node *ptr;
if(front==NULL)
{
printf("Overflow & Stop ");
return;
}
else if(front==rear)
{
free(front);
rear=NULL;
}
else
{
ptr=front;
front=ptr->link;
free(ptr);
}
}