Essential Data Structures home
Stack
A pile where you only ever touch the top. New items go on top, and the last item added is the first one taken off. Last in, first out.
What it is
A stack is a collection with one rule: everything happens at the top. Push puts a new item on top. Pop takes the top item off. Peek looks at the top item without removing it. Items underneath are unreachable until everything above them is gone.
That single rule is the point. By giving up random access, a stack gets a very simple promise: the most recent thing is always the next thing out. The order of removal is the exact reverse of the order of arrival.
A stack is usually built on an array. A Python list used only with append and pop is a stack. It can also be built on a linked list, with the head as the top, which avoids ever resizing an array.
How it works
Push, pop, and peek each touch only the top, so each takes constant time. There is nothing to search and nothing to shift. The only failures are the two edges: pushing onto a full fixed-size stack is an overflow, and popping from an empty stack is an underflow. A dynamic stack grows instead of overflowing, and a careful program checks for empty before it pops.
Recursion is the most famous use. Each function call is pushed onto the call stack, with its local variables, and when the function returns it is popped, so the program resumes exactly where it left off. Too many nested calls fill the stack, which is what a stack overflow error means.
Complexity
| Operation | Average | Worst case |
|---|---|---|
| Push | O(1) | O(1) amortized on a dynamic array |
| Pop | O(1) | O(1) |
| Peek (top) | O(1) | O(1) |
| Search by value | O(n) | O(n) |
Searching a stack means popping items off until the value is found, which destroys the order. A stack is not a search structure.
When to use it, and when not to
Use a stack when the work is naturally nested or reversible: function calls, undo and redo, backtracking in a maze or a search, depth-first traversal, and checking whether brackets balance. If the last thing done should be the first thing undone, a stack is the shape.
Do not use a stack when you need to reach an item in the middle, find something by value, or process items in the order they arrived. That last case is a queue, the stack's mirror image, and reaching into the middle is what an array is for.
Where you meet it
Every program you run uses a call stack. A browser's back button is a stack of pages. Undo in a text editor is a stack of edits. An expression like (3 + 4) * 2 is evaluated with stacks, and a compiler checks that every opening bracket has a matching close with one too.
In Python
stack = []
stack.append("A") # push
stack.append("B") # push
stack.append("C") # push
print(stack[-1]) # peek: C
print(stack.pop()) # pop: C, then B is on top
print(stack.pop()) # pop: B
In short: a stack is discipline, not storage. One end, one rule, and the most recent item always comes out first.
Sources
- Stack (abstract data type), Wikipedia
- Python tutorial, Data Structures (using lists as stacks)
- Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, chapter 10