GATE / CS ARCHIVE

GATE CSE 2012 / QUESTION 35

Empty and full circular queue conditions

Data Structures · Stacks & Queues · Full GATE CSE 2012 paper

GATE 2012, Q35 2 marks · MCQ
Report issue

Stacks & Queues

Question 35, GATE 2012: Suppose a circular queue of capacity (n −1) elements is implemented with an array of n elements.
        Assume that the insertion and deletion operations 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               (B) full: (REAR+1) mod n == FRONT
            empty: REAR == FRONT                            empty: (FRONT+1) mod n == REAR

        (C) full: REAR == FRONT                         (D) full: (FRONT+1) mod n == REAR
            empty: (REAR+1) mod n == FRONT                 empty: REAR == FRONT
Tap to enlarge
Your answer
Answer & solution

Official answer: A

With one of n slots deliberately unused, equal FRONT and REAR denotes the initial empty queue and all subsequent empty states. The queue is full when advancing REAR by one modulo n would collide with FRONT: (REAR+1) mod n=FRONT. These conditions permit n−1 stored elements and distinguish empty from full. Hence A.

Check: For example, with n=4 the usable capacity is three. Starting at FRONT=REAR=0, three insertions advance REAR to 3; its next value would be 0, equal to FRONT, so the full test succeeds without confusing it with the empty state.

Original paper, page 8 · Answer key