IMPLEMENTATION OF QUEUE USING CIRCULAR ARRAY
enqueue(item)
{
rear=(rear +1) mod n;
if(front == rear)
{
pf("full");
if(rear ==0)
rear =n-1;
else
rear = rear-1;
return;
}
else
{q[rear]=item;
return;
}
int dequeue()
{
if(front == rear)
{
pf(ëmpty");
return -1;
}
else
{
front = (front+1) mod n;
item = q[front];
return item;
}
suppose considering array has size of 4 ( indexes 0,1,2,3)
in enqueue rear increments to index 1 and inserts, then increments to 2 and inserts then increments to 3 and inserts
now when it increments to 4, it actually points to 4%4 =0
here front == rear
and since rear is at index 0
it executes rear = rear -1
therefore pointing to index 3
in DEQUEUE front increments to 1 and prints 1st item, then to 2 and prints 2nd item
when it increments to 3
(front == rear) happens and it returns empty without printing the value at index 3 right?
PLEASE HELP