A linear data structure that follows the Last-In, First-Out (LIFO) principle. Elements are added (pushed) and removed (popped) from the same end, called the 'top'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
{
"status": "Initializing stack"
}Overview
A stack is a Last-In, First-Out (LIFO) collection: the last item pushed is the first one popped, like a stack of plates. All operations happen at one end, called the top, which is why push, pop and peek are all O(1).
Stacks are everywhere once you start looking. Your program's call stack stores function frames this way; the undo button keeps a stack of edits; the back button keeps a stack of pages; and many algorithms, from depth-first search to bracket matching, use a stack to remember where to return to.
The default commands push 10, 20 and 30, pop (removing 30), push 40, then pop twice (removing 40 and then 20). Only 10 remains, at the bottom. Each pop always takes whatever was added most recently.
Step by step
push(x): place x on top of the stack.pop(): remove and return the top element; signal an error (underflow) if the stack is empty.peek(): return the top element without removing it.isEmpty(): check whether the stack has no elements.
Complexity
| Case | Cost | Why |
|---|---|---|
| Push | O(1) | Amortised O(1) for a dynamic array; strict O(1) for a linked list. |
| Pop / peek | O(1) | Only the top is touched. |
| Search | O(n) | A stack is not designed for searching. |
| Space | O(n) | One slot per element. |
When to use it
- Matching brackets, tags or parentheses (see the Valid Parentheses interview question).
- Undo/redo, browser history, and backtracking algorithms.
- Evaluating postfix expressions and converting infix to postfix.
- Iterative depth-first search and replacing recursion when the call stack is too small.
Common mistakes
- Popping from an empty stack — always check
isEmpty()or handleundefined. - Using
shift/unshifton a JavaScript array as the stack end; that is O(n). Usepush/pop. - Unbounded recursion is a hidden stack overflow; deep inputs need an explicit stack.
A dry run is the best way to check your understanding of Stack: follow each row, predict the next one, and compare. The run below takes 16 steps in total. The Line column refers to the line numbers in the code listing above.
- Input
- push 10,push 20,push 30,pop,push 40,pop,pop
- Result
- status = Operations complete, final_stack = [10]
| Step | Line | Array | What happens |
|---|---|---|---|
| 1 | 2 | [] | status = Initializing stack |
| 2 | 4 | [] | action = push(10) |
| 3 | 5 | [10] | status = Pushed 10, top = 10 |
| 4 | 4 | [10] | action = push(20) |
| 5 | 5 | [10, 20] | status = Pushed 20, top = 20 |
| 6 | 4 | [10, 20] | action = push(30) |
| 7 | 5 | [10, 20, 30] | status = Pushed 30, top = 30 |
| 8 | 7 | [10, 20, 30] | action = pop() |
| 9 | 11 | [10, 20] | status = Popped 30, top = 20 |
| 10 | 4 | [10, 20] | action = push(40) |
| 11 | 5 | [10, 20, 40] | status = Pushed 40, top = 40 |
| 12 | 7 | [10, 20, 40] | action = pop() |
| 13 | 11 | [10, 20] | status = Popped 40, top = 20 |
| 14 | 7 | [10, 20] | action = pop() |
| 15 | 11 | [10] | status = Popped 20, top = 10 |
| 16 | 18 | [10] | status = Operations complete, final_stack = [10] |
What is a Stack?
What are the primary operations of a Stack?
- Push: Adds an element to the top of the stack.
- Pop: Removes the top element from the stack.
- Peek (or Top): Returns the top element without removing it.
- isEmpty: Checks if the stack is empty.
What is the time complexity of stack operations?
Where are stacks used in computing?
- Function Calls: The 'call stack' manages active function calls. When a function is called, it's pushed onto the stack, and when it returns, it's popped off.
- Undo/Redo Features: Text editors and other software use stacks to implement undo functionality.
- Expression Evaluation: Compilers use stacks to convert expressions from infix to postfix notation and evaluate them.
- Backtracking Algorithms: Stacks are used to keep track of paths in algorithms like maze-solving or in graph traversals (DFS).