Queue

#include
# define max 3

struct queue
{
int queue[max];
int f,r;
}q;

main()
{
int ch,k=1;
q.f=0;
q.r=-1;
while(k==1)
{printf("\t\t MENU\n"
"\t1.INSERT\n"
"\t2.DELETE\n"
"\t3.DISPLAY\n"
"\t4.EXIT\n"
"\tEnter Your Choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
k=0;
exit(0);
default :
printf("Invalid choice");
}
}
}

insert()
{
int val;
if(q.r == max)
{
printf("QUEUE Overflow");
}
else
{
q.r++;
printf("enter the val:");
scanf("%d",&val);
q.queue[q.r]=val;
}
//if(q.f == -1)
// q.f = 0;
}


delete()
{
if(q.f == 0 && q.r ==0)
{
printf("Under flow");
}
printf("Deleted item:%d\n",q.queue[q.f]);
if(q.r == q.f)
{
q.r=q.f=0;
// printf("Deleted item:%d\n",q.queue[q.f]);

}
else
q.f++;
}

display()
{
int i;
//printf("q.f=%d q.r =%d",q.f,q.r);
if(q.r == -1)
printf("EMPTy Queue");
else
{
for(i=q.f;i<=q.r;i++)
{
printf("Queue[%d]=%d\n",i,q.queue[i]);
}
}
}

0 comments: