Understand the LIFO principle, implement a stack, and explore common stack applications.
A stack is a linear data structure that follows LIFO: Last In, First Out. The last item added is the first item removed.
Think of a stack of plates. A clean plate is placed on top, and the next plate is also removed from the top. Reaching a plate in the middle requires removing every plate above it first.
One Point of Access
A stack adds, removes, and reads items at one end, called the top.
Start with an empty stack. In the table, the rightmost value is the top.
| Operation | Stack State | What Happened |
|---|---|---|
push(10) | [10] | 10 is the only item. |
push(20) | [10, 20] | 20 becomes the top item. |
push(30) | [10, 20, 30] | 30 is placed above 20. |
pop() | [10, 20] | 30, the last item added, is removed first. |
The order of insertion is 10 and 20, then 30. The order of removal is reversed: 30, 20, 10.
| Operation | Purpose |
|---|---|
| Push | Add an item to the top. |
| Pop | Remove and return the top item. |
| Peek | Read the top item without removing it. |
| Is Empty | Check whether the stack contains any items. |
Push and pop define the stack's LIFO behavior. Peek and empty checks make the structure safer and easier to use.
JavaScript arrays and Python lists already provide operations for adding and removing items at the end, so either can act as a stack without a separate class.
const stack = [];push adds each new item to the top, represented by the end of the array.
stack.push("first"); // ["first"]
stack.push("second"); // ["first", "second"]pop removes and returns the most recently added item.
console.log(stack.pop()); // "second"
console.log(stack.pop()); // "first"The values come out in the reverse of their insertion order, which confirms the LIFO rule.
const stack = [];
stack.push(1);
stack.push(2);
stack.push(3);
const topItem = stack[stack.length - 1];
const isEmpty = stack.length === 0;
console.log(topItem); // 3
console.log(isEmpty); // false
console.log(stack.pop()); // 3
console.log(stack); // [1, 2]| Operation | Time in the dynamic-array model | Reason |
|---|---|---|
| Push | O(1) amortized | Most pushes write one slot. A resize can copy n items. |
| Pop | O(1) amortized | Removing the last item avoids shifting the others. Some arrays occasionally shrink. |
| Peek | O(1) | Read the top slot. |
| Is empty | O(1) | Read the stored length. |
A stack of n values occupies O(n) space. Check that the stack contains a value before reading or popping its top. Python raises IndexError for an empty pop, while JavaScript's pop() returns undefined.
When a function is called, its execution context—including local variables and the return address—is pushed onto the call stack. When the function finishes, that context is popped so execution can resume at the correct point. The same process helps manage recursive calls.
Applications such as text editors push each action onto a stack. Undo pops the most recent action so changes are reversed in the opposite order from which they occurred.
Each visited page can be pushed onto a history stack. Going back removes the current page and reveals the previously visited page.
Stacks are also useful for reversing data and parsing expressions because both tasks need the most recently stored item first.
push adds, pop removes, peek reads, and isEmpty checks the stack.