Community resourceWorksheet

OCR H446 1.4.2 Stacks: behaviour and operations

Part 3 of 14 · H446 1.4.2 · Data structures

Stacks are examined in H446 1.4.2 as state, not just as a definition, so this worksheet fixes an explicit top-pointer convention and holds students to it. Push, pop and peek are traced on an array stack with a next-free-space pointer, so returned values, overflow and underflow are all decided by pointer state rather than by leftover array contents.

Students will:

  • apply push, pop and peek under a stated next-free-space pointer convention
  • record the array contents, top pointer and returned value after each operation
  • detect overflow and underflow instead of reading a slot that is no longer part of the stack
  • implement a pop function that reports underflow rather than failing
  • explain why last in, first out suits nested calls and undo better than a first in, first out queue

Inside: 7 explanation cells, 2 multiple-choice questions, 2 fill-in-the-blanks cells, 1 written answer, 1 Python task and 1 trace table. 18 marks, about 45 to 55 minutes.

Series: H446 1.4.2 · Data structures, part 3 of 14.

Shared by Coding PathwayVerified teacher

  • 14 cells
  • About 45 minutes
  • CC BY-SA 4.0
  • Shared 31 Aug 2026
  • Updated 15 Sept 2026

Preview

The whole resource, exactly as a class sees it. Answers and marking are held back.

Stacks: behaviour and operations

An undo history removes the most recently added action first. A stack is LIFO: Last In, First Out.

By the end, you will be able to

  • apply push, pop and peek;
  • maintain an explicit top-pointer convention;
  • detect overflow and underflow;
  • read, trace and write an array stack.

Reactivate: a fixed array has indexed capacity.

State the pointer convention

Array stack with next-free-space pointerABCemptyempty01234topStack = 3 (next free space)pop decrements first, then returns stack[topStack].

Here topStack is the index of the next free space. Empty means topStack = 0; full means topStack = capacity. Push stores at topStack then increments. Pop checks non-empty, decrements, then returns stack[topStack].

Worked operations

Initial stack A, B, C with topStack 3.

  • push(D): store D at index 3; topStack becomes 4.
  • pop(): decrement topStack to 3; return stack[3], D.
  • peek(): read stack[topStack − 1], C, without changing topStack.

Returning before decrement/update would end the function and leave later state code unreachable.

Multiple choice1 mark

With a next-free-space topStack, which pop order is correct?

  • ADecrement topStack, then return stack[topStack]
  • BReturn stack[topStack], then decrement
  • CIncrement topStack, then return
  • DShift every item down
Multiple choice1 mark

After a successful array-stack pop, the old value may still be visible in its array slot. Why is it no longer part of the stack?

  • APop must physically erase every array character
  • BtopStack now defines a shorter logical occupied range
  • CThe array has changed into a queue
  • DThe value is automatically encrypted
Trace table5 marks

Trace these push-like operations on a small stack.

Enter a value only when it changes. Follow pointers and output in execution order.

Use one row for each pass through the loop. Fill in a box only when that value changes on that row, and leave the rest blank.

ProgramPython
  1. stack = ["", "", "", ""]
  2. topStack = 0
  3. for item in ["A", "B", "C"]:
  4. stack[topStack] = item
  5. topStack = topStack + 1
  6. print(topStack)
  7. print(stack)
Trace table with 4 columns
RowstacktopStackitemOutput
1
2
3
4
5
6
7

Guided state table

Start with an empty array stack of capacity 3. Use the next-free-space convention. The table gives unchanged or intermediate information so that you can focus on the pointer movement.

Fill in the blanks6 marks
OperationLogical stack, bottom → toptopStack after operationReturned value
push(A)Aentry 1
push(B)A, Bentry 2
pop()Aentry 3entry 4
push(C)A, C2
push(D)entry 53
Another push is entry 6.

Independent transfer: implement pop

Complete pop_stack(data, top). Return a two-item tuple (value, new_top). Return (None, top) on underflow. top is next free.

Coding task4 marks
def pop_stack(data, top):
    if top == 0:
        return None, top

    # top currently identifies the next free position.
    top = ...
    value = ...
    return value, top
Written answer2 marks

Explain why a stack suits nested function calls or undo actions better than a FIFO queue.

Link LIFO behaviour to the scenario order.

Students type their answer here.

Closed-book checkpoint

Complete each sentence from memory. There is no answer bank and correctness is held for teacher review.

Fill in the blanks4 marks
A stack is completion 1: the last item added is removed first. With a next-free pointer, push stores then completion 2 the pointer; pop first completion 3 it, then returns the item. Pop on an empty stack causes completion 4.

Review your work

Check that every push and pop updates the pointer in the correct order and handles the empty case.