Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.
The stack pointer SP holds the index of the top item, and 0 means empty. So the stack is empty when SP is less than 1. Otherwise take the item at ThisStack[SP], then lower SP by 1. Write these in the blanks:
FUNCTION Pop() RETURNS PopData
DECLARE ThisPop : PopData
IF SP < 1 THEN
PopData.Exists ← FALSE // Stack is empty
ELSE
PopData.Data ← ThisStack[SP]
PopData.Exists ← TRUE
SP ← SP - 1
ENDIF
RETURN ThisPop
ENDFUNCTIONDECLARE ThisPop : PopData (the local variable has the type the function returns) [1]
IF SP < 1 THEN (also accepted: SP = 0) [1]
Write FALSE after PopData.Exists in the empty branch and TRUE after PopData.Exists in the ELSE branch. Both must be right [1]
PopData.Data ← ThisStack[SP] [1]
SP ← SP - 1 [1]
▶ Topic lesson
A lesson on this topic · by Papersdock . 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.