GATE / CS ARCHIVE

GATE CSE 2004 / QUESTION 36

One-pointer circular queue

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

GATE 2004, Q36 2 marks · MCQ
Report issue

Stacks & Queues

Question 36, GATE 2004: A queue is represented by a singly linked circular list: front→successive queue nodes→rear→front. One pointer p is available. Where should p point so both enqueue and dequeue take constant time?
Options:
(A) Rear node
(B) Front node
(C) Impossible with one pointer
(D) Node next to front
Question 36, GATE 2004: A queue is represented by a singly linked circular list: front→successive queue nodes→rear→front. One pointer p is available. Where should p point so both enqueue and dequeue take constant time?
Options:
(A) Rear node
(B) Front node
(C) Impossible with one pointer
(D) Node next to front
Tap to enlarge
Your answer
Answer & solution

Answer: A (no official key was published)

1. Keep p at the rear, whose next pointer is the front. Enqueue inserts after p and advances p to the new rear; dequeue removes p.next, with a special empty result for a singleton.

2. Both take constant time, A. Keeping only the front would require finding its predecessor to update the rear link.

3. Teaching check: For enqueue, set new.next=p.next, then p.next=new and p=new. For dequeue, remove p.next and advance that link; if it was the sole node, mark the queue empty. A circular rear pointer therefore exposes both ends without a predecessor scan.

Original paper, page 5