Skip to content
Official PYQ · 2004

GATE CS 2004 Q36 — Linked Lists

Explain with AI

A circularly linked list is used to represent a Queue. A single variable pp is used to access the Queue. To which node should pp point such that both the operations enQueue\text{enQueue} and deQueue\text{deQueue} can be performed in constant time?

Choose one option
View worked solution

Answer: A

To represent a Queue using a circularly linked list with a single pointer pp in O(1)O(1) time for both enQueue and deQueue, the pointer pp must point to the rear node.

Analysis of Options:

  1. If pp points to the rear node (Option A):
    • Front Node: Can be accessed directly in O(1)O(1) time as p->next.
    • deQueue (Deletion from Front):
      The front node is p->next. To delete it, we update the rear's next pointer:
      p->next = p->next->next;
      This takes O(1)O(1) time.
    • enQueue (Insertion at Rear):
      To insert a new node temp at the rear:
      temp->next = p->next; (point new node to front)
      p->next = temp; (point old rear to new node)
      p = temp; (update pp to point to the new rear)
      This takes O(1)O(1) time.
  2. If pp points to the front node (Option B):
    • To perform enQueue, we need to insert at the rear. Finding the rear node requires traversing the entire list from the front, which takes O(n)O(n) time.

Thus, pointing to the rear node allows both operations to be completed in O(1)O(1) constant time.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Linked Lists questions