Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.
Fill in the four gaps. The parameter is the new item, a string. Top goes up by 1 before the item is stored. If the stack is full, output a message.
PROCEDURE Push(NewData : STRING)
IF Top < Max - 1 THEN
Top ← Top + 1
StackArray[Top] ← NewData
ELSE
OUTPUT "Stack full; new data cannot be added"
ENDIF
ENDPROCEDUREParameter written as NewData : STRING [1]
Top ← Top + 1 [1]
StackArray[Top] ← NewData [1]
OUTPUT of a suitable error message such as "Stack full" [1]
Input the item into a variable, then call Push with that same variable.
INPUT MyData CALL Push(MyData)
INPUT into a variable (a prompt is optional) [1]
CALL Push with the input variable as the parameter [1]
A stack is Last In First Out (LIFO), so the last item stored is the first one taken off. [1]
Each time the recursive algorithm calls itself, data (such as the return address and parameters) is pushed onto the stack. [1]
When the base case is reached, the calls start to unwind and the data is popped in the reverse order to which it was pushed. [1]
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.