Revision Notes
AQA A-Level Computer Science (7517): Stacks – Revision Notes
Condensed AQA 7517 section 4.2.3 recall: push, pop, peek, empty and full tests, pointer order, stack uses, overflow and underflow, traps and a self-test.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of data structures
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.2.3 Stacks
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.2.3 Stacks, AQA A-Level Computer Science (7517): push, pop, peek or top, test for empty stack, test for stack full. Peek or top returns the value of the top element without removing it. Algorithms and traced examples are in the stacks study guide; exam-style questions are in the stacks practice questions.
The stack rule
LIFO: last in, first out. One end only, the top. The item removed is the one added most recently.
Array representation
s[0 … maxSize-1] the items
top index of the top item
creation: top ← -1
Alternative convention: top = next free slot, starting at 0. Then empty is top = 0, full is top = maxSize, and Push stores first, then increments. State whichever you use.
The five operations (top starts at −1)
| Operation | Steps | Changes top? |
|---|---|---|
| Is empty | top = −1 | No |
| Is full | top = maxSize − 1 | No |
| Push(item) | Full test; top ← top + 1; s[top] ← item | Yes, +1 |
| Pop | Empty test; item ← s[top]; top ← top − 1; return item | Yes, −1 |
| Peek / top | Empty test; return s[top] | No |
Order matters: push increments then stores; pop reads then decrements.
Trace check
maxSize = 3, stack empty.
| Operation | top after | Stack, bottom → top | Returned or message |
|---|---|---|---|
| Push(A) | 0 | A | |
| Push(B) | 1 | A, B | |
| Push(C) | 2 | A, B, C | |
| Push(D) | 2 | A, B, C | overflow: top = maxSize − 1 |
| Pop | 1 | A, B | C |
| Peek | 1 | A, B | B |
| Pop | 0 | A | B |
| Pop | −1 | empty | A |
| Pop | −1 | empty | underflow |
Peek returned B and the next Pop also returned B, because Peek left it in place.
Overflow and underflow
- Overflow: push onto a full stack.
- Underflow: pop (or peek) from an empty stack.
- Recursion without a base case causes call-stack overflow.
Popped values in memory
A popped value stays in the array until a later push overwrites it, but it is not in the stack, because it lies above top.
Uses
| Use | Why a stack |
|---|---|
| Subroutine calls | Frames (return address, parameters, local variables) must be removed in reverse order of the calls |
| Undo | Most recent action reversed first |
| Reversing data | Push all, pop all: order is reversed |
| Bracket matching | Brackets close in reverse order of opening |
| Reverse Polish evaluation | Operands waiting for an operator are taken most recent first (section 4.3) |
Bracket-matching rules
- Opening bracket → push.
- Closing bracket → if the stack is empty, unbalanced; otherwise pop and check the types match.
- End of text → balanced only if the stack is empty.
Example: ( [ ) ] fails at ) because the popped [ does not match.
Reversal in one line
Push every character of “STACK”, then pop until empty: “KCATS”. The stack must hold at least as many items as the input.
Static vs dynamic stacks
- Static (array): fixed size; needs the full test.
- Dynamic (linked nodes): grows from the heap; empty when the top reference is null; “full” only if memory runs out.
Stack vs queue
| Stack | Queue | |
|---|---|---|
| Rule | LIFO | FIFO |
| Pointers | top | front, rear |
| Removes | Newest item | Oldest item |
Key terms
| Term | Definition |
|---|---|
| Stack | LIFO abstract data type with access only at the top |
| Push | Add an item to the top |
| Pop | Remove and return the top item |
| Peek / top | Return the top item without removing it |
| Top pointer | Index of the top item (or next free slot, by convention) |
| Stack overflow | Pushing onto a full stack |
| Stack underflow | Popping from an empty stack |
| Call stack | Stack of frames for active subroutine calls |
Exam traps
- Peek does not remove the item.
- Full test is
maxSize − 1, notmaxSize, when top starts at −1. - Push: increment before storing. Pop: read before decrementing.
- Test before every operation: full before push, empty before pop and peek.
- A popped value still in the array is not part of the stack.
- A bracket checker must also check the stack is empty at the end.
Self-test
- What does LIFO stand for?
- What is the value of top for an empty stack in the convention above?
- State the full test for a stack of 8 elements.
- In Push, which comes first: incrementing top or storing the item?
- What does Peek return, and what does it change?
- Push(3), Push(5), Pop, Push(1), Peek. What does Peek return?
- After those operations, what does the stack hold, bottom to top?
- Define stack underflow.
- Why is a stack suitable for storing return addresses?
- Push every letter of “TOP” and pop all. What is output?
- What must be true at the end of the text for brackets to be balanced?
- When is a dynamic stack full?
Answers
- Last in, first out.
- −1.
- top = 7.
- Incrementing top.
- The value of the top item; nothing changes.
-
- 3, 1.
- Attempting to pop (or peek) when the stack is empty.
- Subroutines return in the reverse order to which they were called, so the most recent return address is needed first.
- “POT”.
- The stack must be empty.
- Only when the heap has no memory left for a new node.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Trees
Trees, rooted trees, binary trees and binary search trees for AQA A-Level Computer Science 7517 4.2.5, with insertion, searching and array representation.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Trees – Practice Questions
Original AQA A-Level Computer Science 7517 questions on trees, rooted trees, binary search trees and array storage, with mark-scheme-style answers.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Trees – Revision Notes
Condensed AQA A-Level Computer Science 7517 notes on 4.2.5 Trees: definitions, rooted and binary trees, BST rules, array storage, traps and a self-test.
Computer Science · AQA · A 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.