#include <stdio.h>
#include <conio.h>
struct Node
{
int data;
struct Node *link;
};
struct Node *front = NULL;
struct Node *rear = NULL;
int insert(int );
int delete();
void display();
main()
{
int x,opn;
do
{
printf("\n\n\t <1>INSERT <2>DELETE <3>DISPLAY <4>EXIT");
printf("\n\t Enter your option : ");
scanf("%d",&opn);
switch(opn)
{
case 1:
printf("\t Enter number to insert : ");
scanf("%d", &x);
if( insert(x))
printf("\n\t ERROR : OVERFLOW !!!");
printf("\t Press any key to continue ...");
getch();
break;
case 2:
if( front == NULL )
printf("\t ERROR : UNDERFLOW !!!");
else
printf("\t deleted element is : %d", delete());
printf("\n\t Press any key to continue ...");
getch();
break;
case 3:
if( front == NULL )
printf("\n\t QUEUE IS EMPTY");
else
display();
printf("\n\t Press any key to continue ...");
getch();
break;
}
}while(opn!=4);
}
int insert(int x)
{
struct Node *newNode = malloc(sizeof(struct Node));
if(newNode == NULL)
return -1; newNode->data = x;
newNode->link = NULL;
if( front == NULL )
front = newNode;
else
rear->link = newNode;
rear = newNode;
return 0;
}
int delete()
{
int x;
struct Node *tmpNode = front;
if( front == NULL )
return -1;
x = front->data;
front = front->link;
free(tmpNode);
return x;
}
void display()
{
struct Node *tmpNode = front;
if(front == NULL)
return ;
printf("\n\t QUEUE = [ ");
for( ; tmpNode != NULL ; tmpNode = tmpNode->link )
printf(" %d ",tmpNode->data);
printf("]");
}
Output