Open In App
Related Articles

GATE | GATE CS 2012 | Question 33

Like Article
Save Article
Report issue
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

Answer: (A)

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
      Q.tail = Q.tail + 1

  if Q.head == Q.tail
      error "Queue underflow"

  x = Q[Q.head]

  if Q.head == Q.length - 1
      Q.head = 0
      Q.head = Q.head + 1

  return x


This solution is contributed by Pranjul Ahuja

Quiz of this Question

Last Updated : 28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads