#include<stdlib.h>
void main()
{
int front=-1,rear=-1,i,n,item;
int a[5];
clrscr();
while(1)
{
printf("circular queue program\n");
printf("press 1 for insertion\npress 2 for deletion\npress 3 for view the queue\npress for source code\n");
scanf("%d",&n);
switch(n)
{
case 1:
if(front==(rear+1)%5)
{
printf("queue is full\n");
}
else if(front==-1)//front=-1 and rear =-1
{
front=0;
rear=0;
scanf("%d",&item);
a[rear]=item;
}
else if(rear<4)
{
rear=rear+1;
scanf("%d",&item);
a[rear]=item;
}
else if(front!=0 && rear==4)
{
rear=0;
scanf("%d",&item);
a[rear]=item;
}
break;
case 2:
if(front==-1)
printf("queue is empty\n");
else if(front==rear)
{
item=a[front];
printf("delete item from queue is= %d",item);
front=-1;
rear=-1;
}
else if(front==4)
{
item=a[front];
printf("delete item from queue is= %d",item);
front=0;
}
/* else if(front<rear)
{
item=a[front];
printf("delete item from queue is=%d",item);
front=front+1;
}
else if(front>rear)
{
item=a[front];
printf("delete item from queue is=%d",item);
front=front+1;
}
*/
else
{
item=a[front];
printf("delete item from queue is=%d",item);
front=front+1;
}
break;
case 3:
if(front==-1)
printf("queue is empty\n");
if(front<=rear)
{
for(i=front;i<=rear;i++)
printf("%d\n",a[i]);
}
if(rear<front)
{
for(i=0;i<=rear;i++)
printf("%d\n",a[i]);
for(i=front;i<=4;i++)
printf("%d\n",a[i]);
}
break;
case 4:
exit(0);
break;
default :
printf("you have entered wrong choice\n");
break;
}
}
getch();
}