Program to Implement Double Ended Queue
Here is the code of program:-
#include<stdio.h>
#include<conio.h>
void main()
{
int front=0,rear=-1,queue[20],n,data,i=0,ch;
clrscr();
printf("Enter size of Queue: ");
scanf("%d",&n);
printf("1. Insert from Rear.\n");
printf("2. Insert from Front.\n");
printf("3. Delete from Rear.\n");
printf("4. Delete from Front.\n");
printf("5. Display from Rear.\n");
printf("6. Display from Front.\n");
printf("7. Exit.\n");
do
{
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
if((rear>=(n-1))&&(front==0))
{
printf("Overflow\n");
}
else if((rear>=(n-1))&&(front>0))
{
printf("Insertion from Front is possible only.\n");
}
else
{
printf("Enter data: ");
scanf("%d",&data);
rear=rear+1;
queue[rear]=data;
}
break;
case 2:
if((rear>front)&&(front>0))
{
printf("Enter data: ");
scanf("%d",&data);
front=front-1;
queue[front]=data;
}
else if((rear>=(n-1))&&(front==0))
{
printf("Overflow\n");
}
else
{
printf("Enter data: ");
scanf("%d",&data);
for(i=rear+1;i>front;i--)
{
queue[i]=queue[i-1];
}
queue[front]=data;
rear=rear+1;
}
break;
case 3:
if((front==0)&&(rear==-1))
{
printf("Underflow\n");
}
else
{
data=queue[rear];
if(rear>front-1)
{
printf("Deleted item %d\n",data);
}
else
{
printf("Underflow\n");
}
rear=rear-1;
}
break;
case 4:
if((front==0)&&(rear==-1))
{
printf("Underflow\n");
}
else
{
data=queue[front];
if(front<rear+1)
{
printf("Deleted item %d\n",data);
}
else
{
printf("Underflow\n");
}
front=front+1;
}
break;
case 5:
for(i=rear;i>front;i--)
{
printf("%d ",queue[i]);
}
if(i==front)
{
printf("%d\n",queue[i]);
}
else
{
printf("Queue is Empty\n");
}
break;
case 6:
for(i=front;i<rear;i++)
{
printf("%d ",queue[i]);
}
if(i==rear)
{
printf("%d\n",queue[i]);
}
else
{
printf("Queue is Empty\n");
}
break;
case 7:
exit(0);
default:
printf("Invalid option");
}
}while(ch>0&&ch<7);
getch();
}