If the queue is implemented with a linked list, keeping track of a front…

2013

If the queue is implemented with a linked list, keeping track of a front pointer and a rear pointer, which of these pointers will change during an insertion into a non-empty queue ?

  1. A.

    Neither of the pointers change

  2. B.

    Only front pointer changes

  3. C.

    Only rear pointer changes

  4. D.

    Both of the pointers changes

Attempted by 722 students.

Show answer & explanation

Correct answer: C

Answer: Only rear pointer changes for insertion into a non-empty queue.

Reason: When inserting into a non-empty queue implemented with a linked list, you attach the new node after the current rear and then update the rear pointer. The front pointer continues to point to the first element and does not change.

  • Create a new node containing the value to insert.

  • Set the current rear's next pointer to the new node (currentRear.next = newNode).

  • Update the rear pointer to point to the new node (rear = newNode).

  • The front pointer remains unchanged.

Note: If the queue is empty before insertion, then both front and rear are set to the new node. That is the only case where both pointers change.

Explore the full course: Coding For Placement