📄 Paper 1 · 4.2 Data Structures
⭐ Pro
4.2.3 Stacks
AQA 7517 · A-Level Computer Science · ~15 min read

What is a Stack?

A stack is a linear ADT that operates on the LIFO (Last In, First Out) principle — the most recently added item is the first to be removed. Like a stack of plates.

Stack Operations

OperationDescriptionError condition
PushAdd an item to the top of the stackOverflow if full
PopRemove the item from the top of the stackUnderflow if empty
Peek / TopView the top item without removing itUnderflow if empty
isEmpty()Returns TRUE if stack has no items
isFull()Returns TRUE if stack is at capacity

Stack Pointer (SP)

A stack is typically implemented using an array with a stack pointer (SP) that tracks the index of the top item.

  • Push: SP++ then store item at stack[SP]
  • Pop: return stack[SP] then SP--
  • isEmpty: SP < 0 (or SP = -1 if initialised at -1)
// Stack implementation (array-based, max size 5)
stack = [_, _, _, _, _]   // SP = -1 (empty)

Push 10 → SP=0  stack=[10,_,_,_,_]
Push 20 → SP=1  stack=[10,20,_,_,_]
Push 30 → SP=2  stack=[10,20,30,_,_]
Pop     → returns 30, SP=1  stack=[10,20,_,_,_]
Peek    → returns 20, SP unchanged

Applications of Stacks

ApplicationWhy a stack?
Call stack / subroutine callsReturn address and local variables saved on stack; LIFO order means most recent call returned first
Undo functionalityMost recent action undone first (LIFO)
Reverse Polish Notation (RPN) evaluationOperands pushed, operators pop and push results
Bracket matchingOpen brackets pushed, closed brackets matched by popping
Browser back buttonPages pushed when visited; popped to go back
Depth-first search (DFS)Nodes pushed onto stack to explore depth-first

Stack Frames (Revision Link)

When a subroutine is called, a stack frame is pushed onto the call stack containing the return address, parameters, and local variables. When the subroutine returns, the frame is popped. This links stacks directly to 4.1.1g.

Exam tip: Know LIFO, push/pop/peek operations, and the stack pointer. Be able to trace stack operations showing SP changes. Link stacks to real applications — especially the call stack and RPN evaluation. Know overflow (push to full) and underflow (pop from empty).
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.2.3 Stacks

8 questions · instantly marked · AQA 7517 standard

Q1What principle does a stack operate on? Define this principle.[2]
✅ Mark scheme
Mark scheme
LIFO — Last In, First Out [1]; the most recently added item is the first to be removed [1].
Q2State the five main operations of a stack and describe each.[5]
✅ Mark scheme
Mark scheme
Push — add item to top [1]; Pop — remove item from top [1]; Peek/Top — view top without removing [1]; isEmpty() — TRUE if empty [1]; isFull() — TRUE if at capacity [1].
Q3A stack (max size 4) has SP=-1 (empty). Show the state after: Push 5, Push 12, Push 7, Pop, Peek.[5]
✅ Mark scheme
Mark scheme
Push 5 → SP=0, stack=[5] [1]; Push 12 → SP=1, stack=[5,12] [1]; Push 7 → SP=2, stack=[5,12,7] [1]; Pop → returns 7, SP=1, stack=[5,12] [1]; Peek → returns 12, SP unchanged [1].
Q4What is the stack pointer? What value does it hold when a stack is empty (using -1 as initial value)?[2]
✅ Mark scheme
Mark scheme
The stack pointer (SP) is a variable/pointer that stores the index of the top item in the stack [1]; when empty it holds -1 (points to no valid index) [1].
Q5Explain how a stack is used to implement the 'Undo' feature in a text editor.[3]
✅ Mark scheme
Mark scheme
Each user action (e.g. typing a character) is pushed onto the stack [1]; when 'undo' is triggered, the most recent action is popped from the stack [1]; LIFO ensures the most recent action is undone first [1].
Q6How is a stack used when evaluating a Reverse Polish Notation expression?[3]
✅ Mark scheme
Mark scheme
Operands (numbers) are pushed onto the stack [1]; when an operator is encountered, two operands are popped, the operation is applied [1]; the result is pushed back onto the stack [1]; final value remaining on stack is the answer.
Q7Explain the role of the call stack (call stack frames) when a subroutine is called and when it returns.[4]
✅ Mark scheme
Mark scheme
When a subroutine is called, a stack frame is pushed onto the call stack [1]; the frame stores the return address, parameter values and local variables [1]; when the subroutine finishes (RETURN), the stack frame is popped [1]; execution resumes at the return address stored in the popped frame [1].
Q8Define overflow and underflow in the context of a stack.[2]
✅ Mark scheme
Mark scheme
Overflow — attempting to push to a full stack [1]; underflow — attempting to pop from an empty stack [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 10
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — Stacks

10 questions · 10 minutes

← 4.2.2 Queues
13 of 70 · AQA 7517
4.2.4 Graphs →