Stacks
LIFO — Last In, First OutA stack is like a pile of plates: you can only add or remove at the top. The last item pushed is the first one popped. Here the stack is stored in an array with a top index — watch it move as you push and pop.
- Pushadd on top
- Popremove from the top
- topindex of the top item (−1 = empty)
- Top / current
- Pushed / found
- Popping
- Empty slot
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
How is a stack stored?
We use an ordinary array plus one number: top, the index of the top element. An empty stack has top = -1. Pushing moves top up, popping moves it down — nothing else ever moves.
#define CAPACITY 6 int stack[CAPACITY]; // the storage int top = -1; // -1 means the stack is empty // top + 1 = number of items // stack[top] = the item on top
Push
- Check the stack is not full (top < CAPACITY − 1).
- Move top up by one.
- Store the value at stack[top].
- Always O(1) — nothing else moves.
Pop
- Check the stack is not empty (top ≠ −1).
- Read the value at stack[top].
- Move top down by one.
- Return the value. O(1).
Peek, isEmpty, isFull
- Peek returns stack[top] without removing it.
- isEmpty is simply: top == −1.
- isFull is simply: top == CAPACITY − 1.
- All three are O(1).
Balanced brackets
- Read the expression left to right.
- Push every opening bracket ( [ {.
- For a closing bracket, pop and check it matches.
- At the end the stack must be empty.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of items. Push, pop and peek never depend on n.
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Push | O(1) |
O(1) |
O(1) |
Instant |
| Pop | O(1) |
O(1) |
O(1) |
Instant |
| Peek | O(1) |
O(1) |
O(1) |
Instant |
| isEmpty / isFull | O(1) |
O(1) |
O(1) |
Instant |
| Search for a value | O(1) |
O(n) |
O(n) |
Grows with n |
| Balanced brackets (length n) | O(n) |
O(n) |
O(n) |
Grows with n |
Stack using an array vs a linked list
| Array | Linked list | |
|---|---|---|
| Push / pop | O(1) at index top | O(1) at the head |
| Size limit | Fixed capacity — can overflow | Grows until memory runs out |
| Extra memory | None per item | One pointer per item |
| Simplicity | Very simple: array + top | Needs node allocation |
Where stacks are used
| Use | What is pushed | Why a stack? |
|---|---|---|
| Undo in an editor | Each action you make | The last action is undone first |
| Browser back button | Each page you visit | Back goes to the most recent page |
| Function calls | Each call (the call stack) | The last function called returns first |
| Checking brackets | Each opening bracket | The last opened must close first |
Check your understanding
Answer each question — you will see the explanation straight away.
You push 10, then 20, then 30. What does pop() return?
Last In, First Out: 30 was pushed last, so it comes out first.
In an array stack, what is the value of top when the stack is empty?
top is the index of the top item. With no items, it is -1 — one below index 0.
What happens if you push onto a full array stack?
There is no free slot, so the push fails — this is called stack overflow.
Is the expression "( [ ) ]" balanced?
When ")" arrives, the top of the stack is "[", which does not match. The last opened bracket must close first.
After clear() on an array stack, where are the old values?
clear() only sets top = -1 — O(1). The old values stay in memory until new pushes overwrite them.
Stacks quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.