QUEUES Using Arrays
#include<stdio.h>
#include<stdlib.h>
#define size 5
int f,r;
int Q[size];
void qinsert(int x)
{
if(r==size-1)
{
printf("overflow. insertion not possible");
return;
}
if((f==-1)&&(r==-1))
{
f=0;
r=0;
}
else r++;
Q[r]=x;
}
int qdelete()
{
int x;
if((f==-1)&&(r==-1))
{
printf("underflow exists.deletion not possible");
return(-1);
}
x=Q[f];
if(f==r)
{
f=-1;
r=-1;
}
else f++;
return(x);
}
void qtraverse()
{
int i;
if((f==-1)&&(r==-1))
{
printf("queue is empty");
return;
}
for(i=f; i<=r; i++)
printf("%d\t",Q[i]);
}
void main()
{
int x,choice;
f=-1;
r=-1;
while(1)
{
printf("menu 1:insert 2: delete 3:traversal 4: exit");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter element to be inserted");
scanf("%d",&x);
qinsert(x);
qtraverse();
break;
case 2:x=qdelete();
printf("deleted element is %d",x);
printf("remaining queue is");
qtraverse();
break;
case 3:qtraverse();
break;
default: exit(0);
}
}
}