Use arrow keys to navigate · Interactive slides ahead!
What is a Stack?
A stack is a collection with a LIFO access policy:
The last element inserted is the first one removed
Access is restricted to one end called the top
You can only add or remove from the top
Analogy: Stack of Plates
Think of a spring-loaded plate dispenser. You always take the top plate. Clean plates go on top. You never pull from the bottom.
Key Idea
LIFO = Last In, First Out. The most recently added element is always the next one to leave.
Interactive Stack
Push items onto the stack, then try pop and peek!
The Stack ADT (Abstract Data Type)
Core Operations
Method
Description
Time
push(e)
Add element e to top
O(1)
pop()
Remove & return top
O(1)
top() / peek()
Return top without removing
O(1)
isEmpty()
Is the stack empty?
O(1)
size()
Number of elements
O(1)
Key Idea
Every operation is O(1) — constant time. A stack does very little, but does it blazing fast.
Java Interface
public interfaceStack<E> {// Add element to the topvoidpush(E element);// Remove and return the topEpop();// Return top without removingEtop();// Is the stack empty?booleanisEmpty();// Number of elementsintsize();}
Warning
pop() and top() on an empty stack should throw an EmptyStackException (or return null).
Push & Pop Operations
Watch push and pop in action — synced with array-based code highlighting.
Array-Based Push
// t = index of top, starts at -1public voidpush(E element) {if (size() == data.length)throw newFullStackException(); t++; // advance top data[t] = element; // store}
Array-Based Pop
publicEpop() {if (isEmpty())throw newEmptyStackException();E ans = data[t]; // save top data[t] = null; // GC help t--; // shrinkreturn ans;}
Array Visualization
Array capacity: 8 | t = 2 | size = 3
Key Idea
Push is always O(1) — just increment t and store. Pop is O(1) — read, null out, decrement t.
Peek / Top
Look at the top element without removing it
Implementation
// Array-basedpublicEtop() {if (isEmpty())throw newEmptyStackException();return data[t]; // no modification}
Analogy
peek() = looking at the top plate without picking it up. pop() = actually taking it.
top() vs pop()
Aspect
top()
pop()
Returns top?
Yes
Yes
Removes top?
No
Yes
Changes size?
No
Yes (-1)
Destructive?
No
Yes
Visual Comparison
Array-Based Implementation
Store elements in an array, track the top index
Memory Layout
Key Idea
Stack "grows right" in the array. Index 0 is bottom, index t is top. Empty stack: t = -1.
Complete Java Class
public classArrayStack<E> implementsStack<E> {privateE[] data;private int t = -1; // top indexpublicArrayStack(int capacity) { data = (E[]) new Object[capacity]; }public intsize() { return t + 1; }public booleanisEmpty() { return t == -1; }public voidpush(E e) {if (size() == data.length)throw newFullStackException(); data[++t] = e; }publicEpop() {if (isEmpty()) throw newEmptyStackException();E ans = data[t]; data[t] = null; t--;return ans; }publicEtop() {if (isEmpty()) throw newEmptyStackException();return data[t]; }}
Array-Based: When the Array is Full
Dynamic resizing with doubling strategy
Dynamic Resizing Demo
Capacity: 2 | Size: 0 | Push items to see doubling in action
Amortized Analysis
Doubling seems expensive (O(n) copy), but happens rarely.
Key Idea: Amortized O(1)
Total cost of n pushes is O(n), so each push is O(1) amortized. The occasional resize is "paid for" by cheap pushes.
Warning
Never grow by a constant (e.g. +10). That gives O(n) amortized per push. Always double.
Linked-List-Based Implementation
Push and pop at the head of a singly linked list
Push = Insert at Head
Pop = Remove Head
Key Idea
Push/pop at the head of a singly linked list is O(1). No resizing needed. No fixed limit.