Related Articles

Data Structures | Linked List | Question 12

• Difficulty Level : Easy
• Last Updated : 28 Jun, 2021

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

(A) rear node
(B) front node
(C) not possible with a single pointer
(D) node next to front

 `/* p is pointer to address of rear (double pointer).  This function adds new ``   ``node after rear and updates rear which is *p to point to new node  */``void`  `enQueue(``struct` `node **p, ``struct` `node *new_node)``{``    ``/* Missing code to handle base cases like *p is NULL */``      ` `     ``new_node->next = (*p)->next;``     ``(*p)->next = new_node;``     ``(*p) = new_node ``/* new is now rear */``     ``/* Note that p->next is again front and  p is rear */`` ` ` ``}`` ` `/* p is pointer to rear. This function removes the front element and ``    ``returns the dequeued element from the queue */``struct` `node *deQueue(``struct` `node *p)``{``    ``/* Missing code to handle base cases like p is NULL,``        ``p->next is NULL,...  etc */`` ` `    ``struct` `node *temp = p->next;``    ``p->next = p->next->next;``    ``return` `temp;``    ``/* Note that p->next is again front and  p is rear */``}`