Edexcel International Advanced Level Computer Science, point 4.3.1
Definition
A stack is an abstract data type (ADT) in which items are added and removed at one end only, called the top. The last item added is the first removed: LIFO (last in, first out), like a pile of plates.
An ADT describes what a structure does (its operations), not how it is stored. A stack can be built from an array or a list (8.2.5) or from a linked list (14.1.1).
Function and characteristics
- Items can be added or removed only at the top.
- A stack pointer (or top index) keeps track of the top item.
- It may have a fixed maximum size (so it can become full) or grow as needed.
- Operations are fast: adding and removing take constant time.
a. Structure
An array-based stack keeps a pointer to the top:
| Index | 0 | 1 | 2 | 3 | 4 |
|---|---|---|---|---|---|
| Item | 5 | 9 | 14 | ||
| top = 2 |
b. Operations
| Operation | What it does | Typical name |
|---|---|---|
| Insert | Add an item to the top. If the stack is full, it's a stack overflow | push(item) |
| Delete | Remove and return the top item. If the stack is empty, it's a stack underflow | pop() |
| Search | Look at the top item without removing it. There's no way to search below the top: to reach an item further down, the items above it would have to be popped first | peek() |
| (helpers) | Is the stack empty? Is it full? | isEmpty(), isFull() |
Checking for empty or full before an operation avoids errors. With an array, the stack is empty when top = −1 and full when top = size − 1 (4, for a 5-slot array).
Push (insert an item):
- Check whether the stack is full. If it is, report an error (stack overflow) and stop.
- Add 1 to the top pointer, so it points to the next free position.
- Store the item at the position the top pointer now gives.
Pop (delete the top item):
- Check whether the stack is empty. If it is, report an error (stack underflow) and stop.
- Output the item at the position the top pointer gives.
- Take 1 from the top pointer, so it points to the new top item.
Peek reads the item at the top pointer without changing the pointer.
Searching (looking for an item below the top) would mean popping items until it is found, which is slow () and not what a stack is designed for, so a stack has no search operation beyond peek().
Uses
- The call stack for subroutines (1.2.4): return addresses and local variables.
- Undo in an editor, and the back button in a browser.
- Evaluating expressions and reverse Polish notation.
- Checking brackets are balanced.
- Backtracking and depth-first search.
Benefits
- Simple, and push and pop are very fast.
- Naturally fits problems that need reversing or undoing in order.
- Easy to implement with an array or a list.
Drawbacks
- Only the top item is reachable, so searching or reaching the middle is slow.
- A fixed-size stack can overflow, and an empty stack can't be popped.
- Not suitable when items must be handled in the order they arrived (use a queue, 4.3.2).
Hand tracing
Work one operation at a time, and draw the stack after each.
| Operation | Stack after (bottom ... top) | Top pointer | Output |
|---|---|---|---|
push(5) |
5 | 0 | |
push(9) |
5, 9 | 1 | |
push(14) |
5, 9, 14 | 2 | |
pop() |
5, 9 | 1 | 14 |
push(2) |
5, 9, 2 | 2 | |
pop() |
5, 9 | 1 | 2 |
peek() |
5, 9 | 1 | 9 |
Worked example
Task: use a stack to check that the brackets in (a + [b * c]) are balanced, and trace the contents.
Method: push each opening bracket. For each closing bracket, pop and check that it matches. At the end the stack must be empty.
| Character | Action | Stack after |
|---|---|---|
( |
push | ( |
a, + |
ignore | ( |
[ |
push | (, [ |
b, *, c |
ignore | (, [ |
] |
pop [, matches |
( |
) |
pop (, matches |
(empty) |
The stack is empty at the end and every pop matched, so the brackets are balanced. For (a + [b)], the ) would pop [, which doesn't match, so it's not balanced.
Exam tip: say "last in, first out" and name the top. For a trace, redraw the stack after each operation, and show what pop returns. Mention overflow (push on full) and underflow (pop on empty) when asked about errors. In Unit 2 you must also implement it with a fixed-length list (8.2.5).
Check yourself
What does LIFO mean?
Last in, first out: the most recently added item is the first one removed.
What is the difference between pop and peek?
Pop removes the top item and returns it. Peek returns the top item without removing it.
What happens if you pop an empty stack?
A stack underflow error occurs, because there is no item to remove.
Give two uses of a stack
Any two of: the call stack for subroutines; undo; the browser back button; evaluating expressions; checking brackets; backtracking.
After push(3), push(7), pop(), push(1), what does the stack contain, bottom to top?
3, 1.