GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Choose constant-time circular queue pointers

2 questions solved the same way, newest first. Back to Data Structures

How these are solved

Use the circular rear.next=front invariant: retain rear to access front and tail and update only constant many links on insertion/deletion. One external pointer versus two and selection versus invariant are distinct questions.

1 mark · MCQ
Report issue

Stacks & Queues

Question 13, GATE 2017: A circular queue uses singly linked nodes, each with one next pointer, and exactly two external pointers FRONT and REAR. For O(1) insertion and deletion, which general structural conditions hold? I:front.next points to rear. II:rear.next points to front. A:I only. B:II only. C:both. D:neither.
Tap to enlarge
Your answer
Answer & solution

Official answer: B

1. For queue order to follow next links, the circular chain must run FRONT → second → ... → REAR → FRONT. Its closing link is therefore REAR.next=FRONT, statement II.

2. To enqueue node u, set u.next=FRONT, REAR.next=u and REAR=u. To dequeue, save the old front, advance FRONT=FRONT.next and set REAR.next=FRONT. Both require only a fixed number of pointer changes, with empty and singleton cases handled separately.

3. If links instead run backward through queue order, removing the front would require finding its predecessor by traversal. Statement I is not the general rule, even though both statements happen to hold in a two-node circle. Choose B.

Original paper, page 5 · Answer key

2 marks · MCQ
Report issue

One-pointer circular queue

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