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

View worked solution
Answer: A
To represent a Queue using a circularly linked list with a single pointer in time for both enQueue and deQueue, the pointer must point to the rear node.
Analysis of Options:
-
If points to the rear node (Option A):
- Front Node: Can be accessed directly in time as
p->next. - deQueue (Deletion from Front):
The front node isp->next. To delete it, we update the rear's next pointer:p->next = p->next->next;
This takes time. - enQueue (Insertion at Rear):
To insert a new nodetempat the rear:temp->next = p->next;(point new node to front)p->next = temp;(point old rear to new node)p = temp;(update to point to the new rear)
This takes time.
- Front Node: Can be accessed directly in time as
-
If 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 time.
- To perform
Thus, pointing to the rear node allows both operations to be completed in constant time.
Free practice · Your answers on this page are not saved.
