A circular linked list is used to represent a queue. A single variable ‘P’ is…

2021

A circular linked list is used to represent a queue. A single variable ‘P’ is used to access the queue. To which node should ‘P’ point so that both enqueue and dequeue operations can be performed in constant time?

image.pngimage.png

Answer: A. rear nodeKey idea: keep P pointing to the rear node of the circular linked list. Then front = P -> next; rear = P. Enqueue requires quick access to the rear. Dequeue…

  1. A.

    rear node

  2. B.

    front node

  3. C.

    not possible with single pointer

  4. D.

    node next to front

Attempted by 850 students.

Show answer & explanation

Correct answer: A

Key idea: keep P pointing to the rear node of the circular linked list.

Then front = P -> next; rear = P.

  • Enqueue requires quick access to the rear.

  • Dequeue requires quick access to the front.

Enqueue (insert element x):

  1. If P == NULL (queue is empty): create new node; set new.next = new; set P = new.

  2. Else: create new node; set new.next = P.next; set P.next = new; set P = new.

Dequeue (remove front element):

  1. If P == NULL: queue is empty (underflow).

  2. Let front = P.next.

  3. If front == P (only one node): set P = NULL.

  4. Else: set P.next = front.next (unlink front). Return front's value.

Because each operation uses a fixed number of pointer updates (and no traversal), both enqueue and dequeue run in O(1) time.

Explore the full course: Bpsc

Loading lesson…