Data Structures & Algorithms · Revision notes
Stacks & Queues
A stack is LIFO and a queue is FIFO; both support O(1) core operations. Stacks power parsing, recursion and monotonic-stack problems; queues power BFS, scheduling and buffering, with circular arrays avoiding wasted space.
Quick revision
- Stack LIFO O(1); queue FIFO O(1); call stack = recursion
- Postfix eval: pop b then a; infix->postfix by precedence/associativity
- Circular queue: (i+1)%N; full (rear+1)%N==front without a counter
- Monotonic stack: amortized O(n); next greater, histogram (sentinel 0)
- Balanced brackets: stack O(n); single type -> counter O(1)