82 82 votes 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 $\text{enQueue}$ and $\text{deQueue}$ can be performed in constant time? rear node front node not possible with a single pointer node next to front Data Structures gatecse-2004 data-structures linked-list normal + – Kathleen 48.6k views answer comment Share Follow Print See all 8 Comments 8 8 Comments reply Show 5 previous comments Tushar Rana commented Jan 8, 2025 reply Follow flag A circular linked list given, meaning the last address of linked list will point to head or start of linked list. Also this implementation is of a queue. Now from the diagram, rear and front are given, which means following queue property first in first out we are deleting from front and as the queue grows we are adding to rear. Say, first 2 enters in the linked list. Then 3 and then 5 and then 6 enters. Now according to question, The linked list will look like 2->3->5->6 (6 is rear latest added and 2 is front oldest added) And since circular linked list is used 6 points to 2. Now where to point the pointer p such that insertion and deletion takes constant time. Clearly if we point it to front, then deletion is constant time but not the insertion as we need to travel the whole linked list it's n time. But if we point to rear then insertion takes constant time as p is pointing to rear element. And deletion is also constant time as rear points to front so we can go to front directly. Therefore, the p points to rear. 3 3 replyShare duckduck commented Dec 11, 2025 reply Follow flag $$ \begin{aligned} \textbf{P points to Rear:} & \quad \text{It is the same as having a rear pointer; P simply acts as an additional pointer to the rear node.} \\[6pt] \textbf{P points to Front:} & \quad \text{If P points to the front and we perform a dequeue, the front moves to the next node.} \\ & \quad \text{The old front node gets deleted, but P still points to that deleted node,} \\ & \quad \text{so P becomes a dangling pointer.}\\ \end{aligned} $$ correct me, if i am wrong? 0 0 replyShare S_Sandeep commented Sep 17 reply Follow flag @duckduck if P points to front, then it takes O(n) to make rear point to the node next to front so it is not correct answer. 0 0 replyShare Please log in or register to add a comment.
–2 –2 votes I think there is no front or rear pointer here only p pointer so enQueue() and deQueue operation should be this 1.) newNode-->next=p-->next-->next; 2.) p-->next-->next=NULL; 3.) free(p-->next); 4.) p-->next=newNode; One answered Jul 26, 2016 One comment Share Follow See 1 comment 1 1 comment reply akankshadewangan24 commented Jul 9, 2017 reply Follow flag @One yes u r right they were not mentioned about rear and front pointers they only give us pointer P 1 1 replyShare Please log in or register to add a comment.