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

Q80 · 9618/22/O/N/24 Q3

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

Q809618/22/O/N/24original Q3Structured · 7 marks
Question 3 from 9618/22/O/N/24

Worked solution

(a)(i)

The stack grows upwards from index 1 and nothing is on it at the start.

Write: SP = 1 and OnStack = 0 [1]

(a)(ii)

Say why unused elements are never read.

Values in unused elements can never be popped (taken off) [1]

Until a value has been pushed, which overwrites whatever was there [1]

(b)

The stack is full when OnStack is 60 (SP would be 61). Store the value at position SP, then move SP up by one.

FUNCTION Push(ThisValue : REAL) RETURNS BOOLEAN
    DECLARE ReturnValue : BOOLEAN
    IF OnStack = 60 THEN
        RETURN FALSE    // stack is already full
    ENDIF
    ThisStack[SP] ← ThisValue
    SP ← SP + 1
    OnStack ← OnStack + 1
    RETURN TRUE
ENDFUNCTION

First gap: OnStack = 60 (or SP = 61, SP > 60, OnStack > 59) [1]

Second gap: FALSE [1]

Third gap: ThisStack[SP] [1]

Fourth gap: SP + 1 [1]

KEY IDEAA stack pointer shows the next free place: store at SP, then move SP up, and refuse to push when the stack is full.
TOTAL 7 marks

Official mark scheme

Mark scheme for 9618/22/O/N/24 question 3
VIDEO
▶ Whole paper

Video solution of the whole paper · 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.