Stack (LIFO)
push · pop O(1)A stack is a last-in, first-out (LIFO) collection: the most recently added item is the first one out, like a stack of plates. Both operations touch only the top, so each runs in constant time. Stacks power undo histories, the function call stack, and depth-first search.
Stack
Press ▶ to run
Edit the input and press Play
How it works
- push adds a value on top of the stack.
- pop removes and returns the value on top.
- Only the top is reachable — items underneath stay hidden until everything above is popped.
Pseudocode
1push(value): # O(1)2 items.append(value) # add on top34pop(): # O(1)5 if items is empty: error6 return items.removeLast() # take from the top