A linked list Queue

A circularly linked list is used to represent a queue as shown in the figure above. A single variable Q Q is used to access the Queue. To which node should Q Q point such that both the operations enQueue and deQueue can be performed in O ( 1 ) O(1) time?

not possible node after head Head Rear

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Jeremy Rixon
Oct 9, 2018

The deQueue operation requires access to the Head node (to return to the caller) and access to the Rear node (to update the Rear node's pointer to point to the new Head node).

The enQueue operation requires access to the Rear node (to update to the Rear node's pointer to point to the new Rear node).

If Q points to the Rear node, access to the Rear node is O(1) (one step: dereferencing the Q pointer), access to the Head node is also O(1) (two steps: dereferencing the Q pointer and then dereferencing the Rear node's pointer to the Head node).

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...