#include <stdio.h>
#define max 5
typedef struct
{
int a[max],front,rear;
}queue;
void insertLeft(queue *q,int ele)
{
if(q->front==-1)
{
printf("Queue Overflow\n");
return;
}
q->a[q->front]=ele;
q->front--;
}
void insertRight(queue *q,int ele)
{
if(q->rear==max-1)
{
printf("Queue Overflow\n");
return;
}
q->rear++;
q->a[q->rear]=ele;
}
int isempty(queue *q)
{
if(q->front==q->rear)
return 1;
return 0;
}
int deleteLeft(queue *q)
{
int x;
q->front++;
x=q->a[q->front];
return x;
}
int deleteRight(queue *q)
{
int x;
x=q->a[q->rear];
q->rear--;
return x;
}
void display(queue *q)
{
int i;
if(isempty(q))
{
printf("Queue Underflow\n");
return;
}
for(i=q->front+1;i<=q->rear;i++)
printf("%d\t",q->a[i]);
printf("\n");
}
int main(int argc, const char * argv[]) {
// insert code here...
queue q;
q.front=max/2;
q.rear=max/2;
int ch,ele;
while(1)
{
printf("1-InsertLeft\n2-InsertRight\n3-DeleteLeft\n4-DeleteRight\n5-Display\n6-Exit\nEnter Choice=");
scanf("%d",&ch);
if(ch==6)
break;
switch(ch)
{
case 1:
printf("Enter element to Insert at Left=");
scanf("%d",&ele);
insertLeft(&q,ele);
break;
case 2:
printf("Enter element to Insert at Right=");
scanf("%d",&ele);
insertRight(&q,ele);
break;
case 3:
if(isempty(&q))
printf("Queue Underflow\n");
else
{
ele=deleteLeft(&q);
printf("Element Deleted=%d\n",ele);
}
break;
case 4:
if(isempty(&q))
printf("Queue Underflow\n");
else
{
ele=deleteRight(&q);
printf("Element Deleted=%d\n",ele);
}
break;
case 5: display(&q);
break;
default: printf("Invalid Choice\n");
}
}
return 0;
}