Stacks Explained
A stack is a collection that follows LIFO order — last in, first out. You add and remove items from one end only, called the top. The last item you push is the first one you pop, like a stack of plates where you take the top plate first. Both operations run in O(1) time.
The core operations
A stack supports a small, deliberate set of operations:
- push — add an item to the top.
- pop — remove and return the top item.
- peek (or top) — look at the top item without removing it.
- isEmpty — check whether the stack has any items.
The restriction is the point. By forbidding access to the middle, a stack guarantees the LIFO discipline, which is exactly what many problems need.
Why LIFO is useful
Think about an undo feature. Each action you take is pushed onto a stack. When you press undo, the most recent action pops off and reverses — last in, first out. Browser back buttons, expression evaluation, and matching brackets all rely on the same principle.
The most fundamental example is the call stack. When your program calls a function, the computer pushes a frame holding that function's local data. When the function returns, its frame pops. Nested calls stack up and unwind in perfect reverse order, which is why recursion works.
Implementing a stack in Java
// A simple integer stack backed by an array.
// push, pop, and peek are all O(1).
class IntStack {
private int[] data;
private int top; // index of the top element; -1 means empty
public IntStack(int capacity) {
data = new int[capacity];
top = -1;
}
// Add a value to the top of the stack.
public void push(int value) {
if (top == data.length - 1) {
throw new RuntimeException("Stack overflow");
}
data[++top] = value; // move top up, then store
}
// Remove and return the top value.
public int pop() {
if (isEmpty()) {
throw new RuntimeException("Stack underflow");
}
return data[top--]; // read top, then move top down
}
// Look at the top without removing it.
public int peek() {
if (isEmpty()) {
throw new RuntimeException("Stack is empty");
}
return data[top];
}
public boolean isEmpty() {
return top == -1;
}
public static void main(String[] args) {
IntStack s = new IntStack(5);
s.push(10);
s.push(20);
s.push(30);
System.out.println(s.pop()); // 30 (last in, first out)
System.out.println(s.peek()); // 20
System.out.println(s.pop()); // 20
}
}
Each operation touches only the top index, so none of them depends on how many items the stack holds — they are all constant time.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesArray-backed versus linked stacks
You can build a stack on top of an array (as above) or a linked list. The array version is simple and cache-friendly but has a fixed capacity unless you grow it. The linked version grows freely but uses extra memory per node. Both deliver O(1) push and pop.
Common mistakes
- Popping an empty stack. Removing from an empty stack (stack underflow) is a frequent bug. Always check
isEmptyfirst or handle the exception. - Overflowing a fixed array. A static array stack has a limit. Pushing past it (stack overflow) corrupts memory or throws. Track capacity, or use a growable structure.
- Treating it like a list. A stack deliberately hides everything except the top. If you find yourself wanting to peek into the middle, a stack is the wrong tool.
- Confusing push/pop order. The last item pushed is the first popped. Beginners often expect FIFO behaviour and get surprised — that is a queue, not a stack.
- Deep recursion overflow. Because recursion uses the system call stack, very deep recursion can exhaust it and crash. Sometimes an explicit stack avoids that.
FAQ
What is the difference between a stack and a queue? A stack is last-in-first-out; a queue is first-in-first-out. See Queues Explained.
Is the call stack the same idea? Yes. Function calls push frames and returns pop them, which is exactly LIFO behaviour and the reason recursion unwinds correctly.
Keep learning
Contrast this with Queues Explained, see how stacks drive recursion problems, and browse the full DSA hub.
Want hands-on stack problems with instructor feedback in Jalgaon? Join the waitlist for our Data Structures & Algorithms course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays as a Data Structure
A clear introduction to arrays: how contiguous storage gives O(1) random access, why inserting in the middle is O(n), and when to reach for an array versus another structure.
Big-O Notation & Time Complexity, Simply Explained
A plain-language guide to Big-O notation and time complexity, covering the common growth rates with worked examples so you can reason about how fast your code really is.
Binary Search Trees Explained
Understand the binary search tree: how the left-smaller, right-larger rule enables O(log n) search on a balanced tree, and why balance is the whole game.
