ITPEC FE Subject B April 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 deleteLast removes an element at the end of a doubly linked list. Each element of the doubly linked list is represented by the class ListElement. The table shows the description of the class ListElement. The ListElement-type variable holds a reference to an instance of the class ListElement. The global variable listHead holds a reference to the head element of the doubly linked list. Remember that each element in the doubly linked list has a reference to its previous element and its next element. Here, if the list is empty, listHead is set to undefined.
The procedure handles three main cases: if the list is empty, it outputs "empty." If the list contains only one element, it becomes empty after deletion. If multiple elements are present, it only removes the last element.
Table Class ListElement
Answer group
Explanation
A: current.next, B: current.prev.next — walk to the node with no successor, then clear the link that points to it.
The loop advances with current ← current., so it must stop on the last element, the one whose next is undefined: A = current.. The element before it still reaches it through its own next, so removing the last element means clearing that pointer: current.. When the last element has no prev, it is the only one, and the else branch empties the list.
Trace with 10 ⇄ 20 ⇄ 30: current visits 10, 20, 30 and stops, since the next of 30 is undefined. The prev of 30 is 20, so the next of 20 becomes undefined, leaving 10 ⇄ 20.
Answer (e)
Why not others:
- •(a) and (b) — A =
current.: the head has no previous element, so the loop never runs;prev currentstays on the head, whoseprevis undefined, and theelsebranch empties the whole list. - •(c) and (d) — A =
currentruns untilcurrentitself is undefined, socurrent.afterwards reads a member of an undefined reference.prev - •(f) — the right loop, but B =
current.is already undefined on the last element; assigning undefined changes nothing, and 20 still links to 30.next
Key rule: To unlink an element, change the pointer its neighbour holds, not the element's own pointer; the neighbour is how the rest of the list reaches it.
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.