405 views
0 votes
0 votes
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

Please log in or register to answer this question.

Related questions

0 votes
0 votes
1 answer
2
kickassakash asked Jul 4, 2023
420 views
I have specific doubt on this question and I’ve tried to explain that in the picture ,If anyone can explain it then it’ll be of great help. according to sachin sir th...
0 votes
0 votes
1 answer
3
Souvik33 asked Apr 2, 2023
428 views
There are Insert and Retrieve_Max operations on a set {}. for n such operations what is the time complexity of the efficient algorithm possible?$n^{2}$nlogn n logn
0 votes
0 votes
1 answer
4
Souvik33 asked Nov 2, 2022
864 views
Which data structure would be most appropriate to implement a collection of values with the following 3 characteristicsSingly link list with head and tail pointerDoubly l...