# GATE | GATE CS 2012 | Question 33

Suppose a circular queue of capacity (n – 1) elements is implemented with an array of n elements. Assume that the insertion and deletion operation are carried out using REAR and FRONT as array index variables, respectively. Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty are
(A) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
(B) Full: (REAR+1) mod n == FRONT, empty: (FRONT+1) mod n == REAR
(C) Full: REAR == FRONT, empty: (REAR+1) mod n == FRONT
(D) Full: (FRONT+1) mod n == REAR, empty: REAR == FRONT

Explanation:
Implementation of Circular Queue :

Head – It always points to the location from where next deletion takes place from the queue.
Tail – It always points to the next empty location in which next insertion will take place in the queue.

We will be using wrap around feature since it is a circular queue which is when the tail or head is at the index n-1, next operation will bring them to index 0. In Spite of having capacity of n inside array, we will reserve one empty spot in order to detect the overflow(Queue Full) and underflow(Queue Empty) conditions. The elements in the queue reside in locations Q.head, Q.head + 1, . . . , Q.tail + 1, where we “wrap around” in the sense that location 0 immediately follows location n-1 in a circular order.

Algorithm :

```ENQUEUE(Q, x)
{

if Q.head == Q.tail + 1
error "Queue overflow"

Q[Q.tail] = x

if Q.tail == Q.length    - 1
Q.tail = 0
else
Q.tail = Q.tail + 1
}

DEQUEUE(Q)
{
error "Queue underflow"

if Q.head == Q.length - 1
else

return x
}

```

This solution is contributed by Pranjul Ahuja

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.