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

Member VariableTypeDescription
dataintegerThe value of an element.
nextListElementReference to the instance that holds the next element in the list.
prevListElementReference to the instance that holds the previous element in the list.
[Program]
global: ListElement: listHead /* A reference to the first element of
the list is stored. */
○ deleteLast()
ListElement: current
if (listHead is undefined)
output "empty"
else
current ← listHead
while (A is not undefined)
current ← current.next
endwhile
if (current.prev is not undefined) // multiple elements are present
B ← undefined
else // only one element is present
listHead ← undefined // empty list
endif
endif

Answer group

OptionAB

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.next, so it must stop on the last element, the one whose next is undefined: A = current.next. The element before it still reaches it through its own next, so removing the last element means clearing that pointer: current.prev.next ← undefined. 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.prev: the head has no previous element, so the loop never runs; current stays on the head, whose prev is undefined, and the else branch empties the whole list.
  • •(c) and (d) — A = current runs until current itself is undefined, so current.prev afterwards reads a member of an undefined reference.
  • •(f) — the right loop, but B = current.next is already undefined on the last element; assigning undefined changes nothing, and 20 still links to 30.

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.