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<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define max 5
int front=-1;
int rear=-1;
int queue[max];
void insert();
void Delete();
void display();
main()
{
int c1,c2;
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(rear==max-1)
printf("\nQueue is full");
else
{
printf("Enter item: ");
scanf("%d",&item);
if(rear==-1 && front==-1)
rear=front=0;
else
rear=rear+1;
queue[rear]=item;
printf("\nThe inserted item is %d",queue[rear]);
}
}
void Delete()
{
int item;
if(front==-1)
printf("\nQueue is empty");
else
{
item=queue[front];
if(front==rear)
front=rear=-1;
else
front=front+1;
printf("The deleted item is %d",item);
}
}
void display()
{
int i;
printf("Displaying the items\n");
for(i=front;i<=rear;i++)
printf("\t%d",queue[i]);
}
0 comments:
Post a Comment