This Program Shows queue is full if rear has the value equals to (max-1) even when the queue is empty.
This problem is solved in circular queue.
#include<conio.h>
#include<stdlib.h>
#define max 5
struct queue
{
int front;
int rear;
int queue[max];
}q;
void insert();
void Delete();
void display();
main()
{
int c1,c2;
q.front=-1;
q.rear=-1;
clrscr();
do
{
printf("\nEnter the Operation\n");
printf("1.INSERT 2.DELETE 3.DISPLAY 4.EXIT ");
scanf("%d",&c1);
switch(c1)
{
case 1:
insert();
break;
case 2:
Delete();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nEnter valid choice ");
}
printf(" [Press 1 to continue] ");
scanf("%d",&c2);
}
while(c2==1);
if(c2!=1)
exit(0);
getch();
}
void insert()
{
int item;
if(q.rear==max-1)
printf("\nQueue is full");
else
{
printf("Enter item: ");
scanf("%d",&item);
if(q.rear==-1 && q.front==-1)
q.rear=q.front=0;
else
q.rear=q.rear+1;
q.queue[q.rear]=item;
printf("\nThe inserted item is %d",q.queue[q.rear]);
}
}
void Delete()
{
int item;
if(q.front==-1)
printf("\nQueue is empty");
else
{
item=q.queue[q.front];
if(q.front==q.rear)
q.front=q.rear=-1;
else
q.front=q.front+1;
printf("The deleted item is %d",item);
}
}
void display()
{
int i;
printf("Displaying the items\n");
for(i=q.front;i<=q.rear;i++)
printf("\t%d",q.queue[i]);
}
0 comments:
Post a Comment