3
7
2
9
[0][1][2][3]
top ↓
(empty)
stack
Concept
▸1stack = [] // LIFO container2push(x): place x on TOP3pop(): remove and return the TOP4peek(): read the TOP without removing5// every operation is O(1)
state
- orderLIFO
Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.
Try one problem▸1stack = [] // LIFO container2push(x): place x on TOP3pop(): remove and return the TOP4peek(): read the TOP without removing5// every operation is O(1)
line 1A stack is the simplest container with a personality: you may only touch the TOP. Add with push, remove with pop — the LAST thing in is the FIRST thing out (LIFO).