Cambridge AS Level Computer Science 9618 · 10.4 Introduction to Abstract Data Types (ADT)

Q85 · 9618/23/O/N/25 Q3

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.

Q859618/23/O/N/25original Q3Structured · 5 marks
Question 3 from 9618/23/O/N/25

Worked solution

(a)

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
ENDFUNCTION

DECLARE 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]

KEY IDEATo pop, test for an empty stack first, then read the top item at SP and decrease SP by 1.
TOTAL 5 marks

Official mark scheme

Mark scheme for 9618/23/O/N/25 question 3
VIDEO
▶ 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.