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: 8 explanation cells, 2 multiple-choice questions, 2 fill-in-the-blanks cells, 1 written answer, 2 Python tasks and 1 trace table. 22 marks, about 50 to 60 minutes.
Series: H446 1.4.2 · Data structures, part 3 of 14.
Shared by Coding PathwayVerified teacher
- 16 cells
- About 60 minutes
- CC BY-SA 4.0
- Shared 31 Aug 2026
- Updated 1 Oct 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Stacks: behaviour and operations
A stack removes the most recently added item first. This behaviour is called last in, first out (LIFO). It suits an undo history because the latest action is the first one that must be reversed.
By the end, you will be able to
- apply push, pop and peek;
- use a clearly stated top-pointer convention;
- detect overflow and underflow;
- read, trace and write an array-backed stack.
State the pointer convention
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.
With a next-free-space topStack, which pop order is correct?
- AReturn stack[topStack], then decrement
- BDecrement topStack, then return stack[topStack]
- CIncrement topStack, then return
- DShift every item down
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
- BThe array has changed into a queue
- CtopStack now defines a shorter logical occupied range
- DThe value is automatically encrypted
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.
stack = ["", "", "", ""]topStack = 0for item in ["A", "B", "C"]:stack[topStack] = itemtopStack = topStack + 1print(topStack)print(stack)
| Row | stack | topStack | item | Output |
|---|---|---|---|---|
| 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.
| Operation | Logical stack, bottom → top | topStack after operation | Returned value |
|---|---|---|---|
| push(A) | A | entry 1 | none |
| push(B) | A, B | entry 2 | none |
| pop() | A | entry 3 | entry 4 |
| push(C) | A, C | 2 | none |
| push(D) | entry 5 | 3 | none |
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.
def pop_stack(data, top):
if top == 0:
return None, top
# top currently identifies the next free position.
top = ...
value = ...
return value, topApply the opposite operation: implement push
Complete push_stack(data, top, value). The pointer still identifies the next free position. Return the updated pointer and True after a successful push. If the stack is full, leave it unchanged and return the original pointer and False.
def push_stack(data, top, value):
if top == len(data):
return top, False
# Store in the next free position, then update the pointer.
data[...] = value
top = ...
return top, TrueExplain 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.
Review your work
Check that every push and pop updates the pointer in the correct order and handles the empty case.