algo·visual

Stack

Data Structures
avgO(1)worstO(1)spaceO(n)
4
15← top
capacity 8

A stack of 2; 15 is on top

1 / 13
Starting contents2 values
Operations8 / 40
  1. 1. push(23)
  2. 2. push(42)
  3. 3. pop()
  4. 4. push(8)
  5. 5. push(16)
  6. 6. pop()
  7. 7. pop()
  8. 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)

Search algorithms

Jump to any algorithm or page