Theory
Why this matters
Every time you press Ctrl+Z to undo, click your browser's Back button, or one function calls another, a stack is quietly doing the work. It is one of the simplest data structures, and one of the most used. Master it once and a lot of later topics (recursion, expression evaluation, parsing) suddenly click.
Theory
Think of a stack of plates
You add a clean plate to the top of the pile, and when you need one you take it from the top too. You never pull a plate from the middle. The last plate you put on is the first one you take off. That single rule is the whole idea: LIFO, Last In, First Out.
Theory
The core idea
A stack is a linear list with one restriction: all action happens at one end, called the top.
- A pointer (often called
top) remembers where the top of the stack is. - You can only insert or delete at the top, never in the middle or at the bottom.
- Because of this, the last element added is always the first removed (LIFO).
Theory
The operations
- Push, put a new item on the top.
topmoves up by one. - Pop, remove the top item and return it.
topmoves down by one. - Peek / Peep / Display, look at the top item without removing it.
- isEmpty, is the stack empty? (
toppoints to nothing) - isFull, for a fixed-size array stack, is there no room left?
Practical
Visualize it
This step has an interactive visualizer in Gri-Learn on the web.
Think first
Trace it in your head
Start with an empty stack. You push 5, push 9, pop, then push 2. Take five seconds: what was popped, and what is on top now?
Show the answer
The pop removed 9, the most recently pushed item (LIFO). After pushing 2, the stack from bottom to top is 5, 2, so the top is 2. If you said the pop removed 5, that is a queue's behaviour (FIFO), not a stack's.
Watch out
Two errors to know for the exam
Overflow happens when you push onto a stack that is already full (array-based stacks have a fixed size). Underflow happens when you pop (or peek) from an empty stack. Good code checks isFull() before a push and isEmpty() before a pop.
Watch
Data structures: Introduction to Stack
Watch on YouTube · mycodeschool
Theory
How it is implemented
The simplest implementation uses an array plus an integer top index that starts at -1 (empty).
- push(x): if not full, do
top = top + 1thenarr[top] = x. - pop(): if not empty, read
arr[top]then dotop = top - 1. - peek(): return
arr[top](don't changetop).
Try a working version below, change it, run it, and watch the output.
Practical
A tiny array-based stack, run and tweak it
// A simple fixed-size stack. Press Run, then try changing the values.
class Stack {
constructor(capacity) {
this.items = [];
this.capacity = capacity;
}
isFull() { return this.items.length === this.capacity; }
isEmpty() { return this.items.length === 0; }
push(x) {
if (this.isFull()) { console.log("Overflow! cannot push", x); return; }
this.items.push(x);
console.log("push", x, "-> top is now", this.peek());
}
pop() {
if (this.isEmpty()) { console.log("Underflow! stack is empty"); return; }
const x = this.items.pop();
console.log("pop ->", x);
return x;
}
peek() { return this.items[this.items.length - 1]; }
}
const s = new Stack(3);
s.push(10);
s.push(20);
s.push(30);
s.push(40); // full -> overflow
s.pop();
s.pop();
console.log("top after two pops:", s.peek());
This example runs in Gri-Learn on the web, where you can edit it and see the output.
Quiz
A stack of capacity 3 is empty. You run: push A, push B, push C, push D, pop. What does peek() return now?
- B
- C
- D
- A
Show the answer
B
push D arrives when the stack is already full, so it is rejected (Overflow) and changes nothing. The pop then removes C, the top. What remains is A, B, so peek() returns B. If you answered C you missed the overflow; D never got in at all.
Formula
Where stacks show up
Function calls (the "call stack"), undo/redo, browser history (Back), checking balanced parentheses ()[]{}, and converting/evaluating expressions (infix → postfix / prefix). These are classic exam applications, remember at least three.
Summary
Key takeaways
- A stack is a LIFO list, all work happens at the top.
- Push adds to the top; Pop removes from the top; Peek/Peep reads the top without removing it.
- Overflow = push on a full stack; Underflow = pop on an empty stack.
- Array implementation: a
topindex starting at -1, moved up on push and down on pop. - Used for: call stack, undo/redo, browser back, balanced parentheses, infix→postfix/prefix.