ITPEC FE Subject B October 2025 Question 10
From the answer group below, select the correct combination of answers to be inserted into A and B in the program.
The procedure insertAfter inserts an element into a singly linked list at the position after an existing element with a specific data value. The argument targetData is a string type data that represents the value of the existing element in the list, after which a new element is to be inserted. If no corresponding element exists, no action is taken. The argument newData is also a string type data represents the value of the new element to be inserted.
The class Element represents each element of the linked list. The figure explains the constructor and the member variables of the class Element. Element-type variables store references to instances of the class Element. A reference to the first element in the list is pre-stored in the global variable head.
Figure Class Element
Answer group
Explanation
A: not undefined, B: x.next — walk the list until x runs off the end, and splice the new element in between x and its old successor.
- •
xstarts atheadand moves on withx ← x.; after the last element it becomes undefined. The loop must run whilenext xstill refers to an element, so the condition readsx is not undefined. - •To insert
yafterx,ymust first take over the link fromxto the rest of the list, and only then mayxlink toy. The program runsx.right after the blank, so the blank must save the old successor: B isnext ← y x..next
Example: for P → Q → R and insertAfter(, at x = Q the new element N gets next R, then Q gets next N, giving P → Q → N → R.
Answer (d)
Why not others:
- •(f), (g), (h), (i) and (j) — A
undefinedruns the loop only whilexis undefined;xstarts at the first element, so the body never runs and nothing is inserted - •(a) — B
headmakes the new element point back to the first element, forming a cycle and losing everything afterx - •(b) — B
xmakesxandypoint to each other, a two-element cycle, and the rest of the list is lost - •(c) — B
x.is a string, not a reference to andata Element - •(e) — B
x.skips the old successor ofnext. next x, dropping it from the list, and fails whenxis the last element
Key rule: When inserting into a linked list, set the new element's next before changing the existing element's next, or the rest of the list is lost.
Linked List · AI-written, may contain errors
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.