Revision Notes
OxfordAQA A-Level Computer Science (9645): Stacks – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.2.4 Stacks: LIFO, push, pop and peek, both pointer conventions, empty and full tests, uses and a self-test.
- Subject
- Computer Science
- Level
- AS LEVEL
- Topic
- Topic 2 – Fundamental data structures
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to OxfordAQA A Level Computer Science (9645), 2024-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): OxfordAQA A Level Computer Science.
Syllabus points this page covers
9645 (AS Level)
- 3.2.4 Stacks
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (9645).
These notes condense sub-topic 3.2.4 Stacks of OxfordAQA International AS and A-level Computer Science (9645), Version 1.1 (AS content in topic 3.2 Fundamental data structures). For code, traces and the bracket-matching algorithm, see the stacks study guide; then practise with the stacks practice questions.
What the specification requires
- A stack is a last-in first-out (LIFO) structure.
- Apply push, pop and peek (also called top).
- Peek returns the top (most recently pushed) value without removing it.
- Describe situations where a stack is appropriate.
- Implement a stack in a one-dimensional array, with empty and full tests.
- Use a library implementation of a stack.
Key terms
| Term | Meaning |
|---|---|
| LIFO | The last item added is the first removed |
| Top | The only end where items are added and removed |
| Push | Add an item to the top |
| Pop | Remove and return the top item |
| Peek / top | Return the top item without removing it |
| Stack pointer | The variable that tracks the top position |
| Overflow | Pushing onto a full stack |
| Underflow | Popping from (or peeking at) an empty stack |
| Stack frame | Data pushed for a subroutine call: return address, parameters, local variables |
Pointer conventions
top (index of top item) |
next_free (next empty slot) |
|
|---|---|---|
| Start | −1 | 0 |
| Push | top ← top + 1, then store at top | store at next_free, then next_free ← next_free + 1 |
| Pop | read at top, then top ← top − 1 | next_free ← next_free − 1, then read |
| Peek | read at top | read at next_free − 1 |
| Empty | top = −1 | next_free = 0 |
| Full | top = size − 1 | next_free = size |
| Number of items | top + 1 | next_free |
Use whichever convention the question sets up; check the starting value first.
Worked reminder: a top trace
Size 3, top = −1. Push K, push M, pop, push N, push P, push R, peek.
| Operation | Returns | top | Logical stack (bottom → top) |
|---|---|---|---|
| push K | 0 | K | |
| push M | 1 | K, M | |
| pop | M | 0 | K |
| push N | 1 | K, N | |
| push P | 2 | K, N, P (full) | |
| push R | overflow | 2 | K, N, P |
| peek | P | 2 | K, N, P |
Every pop or peek returns the most recently pushed item still in the stack.
When a stack fits
| Situation | Why LIFO |
|---|---|
| Subroutine calls | The most recent call returns first |
| Undo in an editor | The latest action is reversed first |
| Browser back button | Returns to the most recently visited page |
| Reversing a list or string | Popping returns items in reverse order |
| Bracket matching | The last bracket opened must be closed first |
| Backtracking | Return to the most recent choice point |
Bracket-matching algorithm in words
- Read each character.
- Opening bracket: push it.
- Closing bracket: if the stack is empty, or the popped bracket is the wrong type, the text is unbalanced.
- At the end, the text is balanced only if the stack is empty.
Library stacks
| Library | Push | Pop | Peek |
|---|---|---|---|
Python list |
append |
pop |
s[-1] |
Python collections.deque |
append |
pop |
d[-1] |
Python queue.LifoQueue |
put |
get |
(none) |
C# Stack<T> |
Push |
Pop |
Peek |
Empty pops: Python raises IndexError (list, deque); C# throws InvalidOperationException.
Exam traps
- Bottom versus top. Never pop from index 0 of the array.
- Full test with the wrong convention.
top = size − 1andnext_free = sizeare not interchangeable. - Step order. Push and pop must update the pointer and the array in the order your convention needs.
- Peek removes nothing. The pointer does not change.
- Popped values stay in the array until overwritten; they are no longer in the stack.
- Library lists never fill, so a “full” test only applies to the fixed-size array version.
Quick self-test
- What does LIFO stand for?
- State the difference between pop and peek.
- A stack uses
top, starting at −1, in an array of size 8. Give the full test. - In the
next_freeconvention, what doesnext_freeequal besides the next empty index? - What is underflow?
- Name the three items stored in a stack frame.
- Give one situation where a stack is appropriate, with a reason.
- Push X, push Y, pop, push Z, peek. What does the peek return?
- Which Python list expression peeks at the top?
- Why does the bracket checker return False for “((x)”?
- Name the C# method that removes the top item.
- A stack with
top= 4 is popped twice. What istopnow?
Answers
- Last in, first out.
- Pop removes the top item and returns it; peek returns it without removing it.
top = 7(size − 1).- The number of items in the stack.
- Trying to pop from an empty stack.
- Return address, parameters and local variables.
- Undo: the most recent action must be reversed first (or any valid example with reason).
- Z.
s[-1]- One opening bracket remains on the stack at the end.
Pop()-
Official syllabus
OxfordAQA International AS and A-level Computer Science (9645) specification, Version 1.1, for International AS exams May/June 2025 onwards and International A-level exams May/June 2026 onwards: sub-topic 3.2.4 Stacks.
Related resources
-
Study guides
OxfordAQA A-Level Computer Science (9645): Queues
OxfordAQA 9645 sub-topic 3.2.3 in depth: FIFO queues, shuffling and circular array queues, empty and full tests, traces and Python and C# libraries.
Computer Science · OxfordAQA · AS Level
Read guide -
Practice questions
OxfordAQA A-Level Computer Science (9645): Queues – Practice Questions
Twelve original OxfordAQA 9645 questions on sub-topic 3.2.3 Queues: linear and circular traces, empty and full tests and library queues, with full answers.
Computer Science · OxfordAQA · AS Level
Try questions -
Revision notes
OxfordAQA A-Level Computer Science (9645): Queues – Revision Notes
Recall notes for OxfordAQA 9645 sub-topic 3.2.3 Queues: FIFO, pointer conventions, empty and full tests, circular wrap, library methods and a self-test.
Computer Science · OxfordAQA · AS Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.