A stack is one ended linear data structure which models a real world stack by having two primary operations, namely push and pop. (LIFO)

When and where stacks are used
- used by undo mechanisms in text editors.
- used in compiler syntax checking for matching brackets and braces.
- can be used to model a pile of books or plates.
- used behind the scenes to support recursion by keeping track of previous function calls.
- can be used to do a Depth First search (DFS) on a graph.
| Pushing |
O(1) |
| Popping |
O(1) |
| Peeking |
O(1) |
| Searching |
O(n) |
| Size |
O(1) |
Check tower of Hanoi
Stack Code