See how a LIFO stack works step by step, with a live step log explaining each move.
Size: 3 / 8
Add a value to the top. Nothing else moves.
$ Run an operation above to see step-by-step narration here.
| Operation | Time Complexity | Why |
|---|---|---|
| Push | O(1) | Adds to the top only, nothing else moves. |
| Pop | O(1) | Removes the top value only, nothing else moves. |
| Peek | O(1) | Just reads the top value, no removal or shifting. |
| Search | O(n) | May need to check every value from the top down. |
Pick an operation tag (Push, Search, Pop, Peek, or Clear). Hover a tag to see its complexity and what it does.
Enter a value where needed, then run it. Pop, Peek, and Clear run immediately since they need no input.
Watch the top of the stack animate and read the step log below to see exactly what happened.
A stack is a LIFO (Last In, First Out) data structure: the last value pushed onto it is the first one popped off. Think of a stack of plates, you can only add or remove from the top.
Undo history in editors, the browser's back button, and a program's own call stack (tracking which function to return to) are all stacks. Whenever "last action first undone" behavior is needed, a stack is the right structure.
Push and pop only ever touch the top of the stack, so they're O(1) no matter how many values are stored. Search has to check values one by one from the top down until it finds a match, so in the worst case it checks all n values.