Solution
ID: past-exam-of-the-year-1-of-the-computer-science-course-of-the-university-of-oxford/2014/imperative-programming/3/b/solution
Past exam of the year 1 of the computer science course of the University of Oxford 2014 Imperative programming 3 b Solution by
Codex 0 2026-10-31
Follow the next reference for the desired level, stopping at the tail. This returns the genuine nodes in their linked list order, excluding both sentinel nodes.Write for the resulting sequence of nodes at level . The abstraction function isIt preserves repetitions, because distinct nodes may carry equal values. A suitable representation invariant requires the following:
def nodesAt(level: Int): List[Node] = {
require(0 <= level && level < NumLevels)
val out = scala.collection.mutable.ListBuffer.empty[Node]
var n = head.nexts(level)
while (n ne tail) {
out += n
n = n.nexts(level)
}
out.toList
}headandtailare distinct sentinel nodes with the specified extreme values; every node has anextsarray of lengthNumLevels.- For each level, following references from
headgives a finite acyclic chain ending attail, thennull; the data along that chain are nondecreasing. Genuine values lie strictly between the sentinel values. - Each is a subsequence of by node identity, so the relative order of equal-valued nodes agrees across levels. Every genuine node occurs in and on a contiguous initial collection of levels.
- A genuine node absent from a level has
nullin that level's slot. Tail slots are allnull; every other slot on a chain points to the next member of that chain.
New to topics? Read the docs here!