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.

ConstructorDescription
Element(string: data)Initialize the element with the data passed as an argument, which stored in the member variable data, with the member variable next set to undefined.
Member variableTypeDescription
datastringData associated with the element.
nextElementThis attribute represents the reference to the next element.

Figure Class Element

[Program]
global: Element: head // stores first element in the list
○ insertAfter(string: targetData, string: newData)
Element: x, y
x ← head
while(x is A)
if (x.data = targetData)
y ← Element(newData)
y.next ← B
x.next ← y
exit the while block
else
x ← x.next
endif
endwhile

Answer group

OptionAB

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.

  • •x starts at head and moves on with x ← x.next; after the last element it becomes undefined. The loop must run while x still refers to an element, so the condition reads x is not undefined.
  • •To insert y after x, y must first take over the link from x to the rest of the list, and only then may x link to y. The program runs x.next ← y right after the blank, so the blank must save the old successor: B is x.next.

Example: for P → Q → R and insertAfter("Q", "N"), 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 undefined runs the loop only while x is undefined; x starts at the first element, so the body never runs and nothing is inserted
  • •(a) — B head makes the new element point back to the first element, forming a cycle and losing everything after x
  • •(b) — B x makes x and y point to each other, a two-element cycle, and the rest of the list is lost
  • •(c) — B x.data is a string, not a reference to an Element
  • •(e) — B x.next.next skips the old successor of x, dropping it from the list, and fails when x is 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.