ITPEC FE Subject B April 2024 Question 10
ITPEC FE Subject B April 2024 — Question 10 of 20
Inserting into a circular linked list by traversing to the tail node.
In a circular linked list, the last element's next points back to listHead. To insert after the last element, we must first find the tail — the node whose next equals listHead.
Finding the tail (blanks A and B):
The while loop must stop when we reach the node pointing back to the head:
- •A = `listHead`: the condition
while (tmp.next is not listHead)traverses until we find the tail node - •B = `tmp.next`: inside the loop,
tmp ← tmp.nextadvances to the next node
After the loop, tmp is the last element. Then:
- tmp.next ← newNode — tail now points to the new node
- newNode.next ← listHead — new node points to head, closing the circle
Why not others:
- (a) A=newNode, B=listHead.next — the loop condition tmp.next is not newNode makes no sense since newNode was just created and isn't in the list yet; listHead.next doesn't traverse the list
- (c) A=tmp, B=listHead — tmp.next is not tmp is a meaningless self-comparison; tmp ← listHead resets instead of advancing
- (d) A=listHead, B=newNode.next — correct loop condition, but tmp ← newNode.next doesn't advance through the list since newNode isn't linked yet
Key rule: In a circular linked list, the tail node is identified by tail.next == head.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.