Skip to main content
EnglandComputer ScienceSyllabus dot point

How does a stack work and why is it used for calls and recursion?

Understand the stack abstract data type, LIFO behaviour, the push, pop and peek operations using a stack pointer, and the use of stacks for subroutine calls and recursion.

A focused answer to AQA A-Level Computer Science 4.2.3, covering the stack abstract data type, LIFO behaviour, the push, pop and peek operations with a stack pointer, overflow and underflow, and the use of the call stack.

Generated by Claude Opus 4.88 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The stack abstract data type
  3. Overflow and underflow
  4. The call stack, calls and recursion

What this dot point is asking

AQA wants you to describe a stack as an abstract data type with last-in first-out behaviour, trace the push, pop and peek operations using a stack pointer, explain overflow and underflow, and explain why the call stack is used for subroutine calls and recursion.

The stack abstract data type

The operations are push (add an item to the top and increment the pointer), pop (remove and return the top item and decrement the pointer), peek (read the top item without removing it), isEmpty and isFull. Because every operation works on the top, all of them are constant time O(1)O(1), which is part of why stacks are so widely used.

PUSH(item):
  IF NOT isFull THEN
    top = top + 1
    stack[top] = item

POP():
  IF NOT isEmpty THEN
    item = stack[top]
    top = top - 1
    RETURN item

Overflow and underflow

The call stack, calls and recursion

Stacks appear in many other places once you recognise the pattern: the undo feature in an editor pushes each action so the most recent can be undone first; a web browser's back button pops the most recently visited page; and depth-first traversal of a graph uses a stack (or the call stack via recursion) to remember where to backtrack to. In every case the defining feature is that the item you most recently set aside is the one you need back first.

A stack can be implemented either statically, in a fixed-size array with an integer stack pointer as shown above, or dynamically, as a linked list where pushing adds a node at the head and popping removes it. The array version is simple and fast but has a fixed capacity, so it can overflow; the linked version grows as needed but uses extra memory for pointers. Either way the abstract behaviour (LIFO, with push, pop and peek) is identical, which is exactly what it means to call a stack an abstract data type: the specification describes what it does, and a programmer chooses how to build it.

Expression evaluation is a classic exam application. To evaluate a reverse Polish (postfix) expression, operands are pushed onto a stack; when an operator is read, the top two operands are popped, the operation is applied, and the result is pushed back. This works precisely because the most recently seen operands are the ones the operator needs, matching the LIFO order, and it is the reason compilers and calculators convert infix expressions to postfix and evaluate them with a stack.

Exam-style practice questions

Practice questions written in the style of AQA exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

AQA 20194 marksA stack is implemented in an array with a stack pointer initially set to -1 (empty). The operations push(5), push(8), pop(), push(3) are carried out in order. State the contents of the stack and the value of the stack pointer after each operation.
Show worked answer →

Start: stack empty, pointer = -1.

push(5): pointer becomes 0, stack = [5]. push(8): pointer becomes 1, stack = [5, 8]. pop(): returns 8 (the top), pointer becomes 0, stack = [5]. push(3): pointer becomes 1, stack = [5, 3].

Final stack = [5, 3] with the stack pointer at index 1, and the value 8 was returned by the pop.

Markers reward incrementing the pointer on push and decrementing on pop, placing items at the top, and recognising that pop removes the most recently pushed value (8) because the stack is LIFO.

AQA 20213 marksExplain why a stack is the appropriate data structure for managing subroutine calls and recursion.
Show worked answer →

When a subroutine is called, the processor pushes a stack frame containing the return address, parameters and local variables onto the call stack. Because subroutine calls nest (the most recently called subroutine must finish and return before the one that called it can continue), the order of returns is the reverse of the order of calls.

A stack is last-in first-out, so the most recently pushed frame is the first popped, which exactly matches this reverse order. Recursion is repeated nested calling, so each recursive call pushes a new frame and each return pops one, allowing the machine to keep track of every level and resume correctly.

Markers reward linking the LIFO order to the nested call and return order, and mentioning that each frame holds the return address and local variables.

Related dot points

Sources & how we know this