Stack
Data StructuresavgO(1)worstO(1)spaceO(n)
4
15← top
A stack of 2; 15 is on top
1 / 13
Starting contents2 values
Operations8 / 40
- 1. push(23)
- 2. push(42)
- 3. pop()
- 4. push(8)
- 5. push(16)
- 6. pop()
- 7. pop()
- 8. push(37)
How it works
Last in, first out: push adds on top and pop takes from the top, both in O(1). Undo, expression parsing, depth-first search and the call stack itself all use stacks. This one has a fixed capacity, so it can also overflow and underflow.
Complexity
- Best
- O(1)
- Average
- O(1)
- Worst
- O(1)
- Space
- O(n)