A stack is a collection with LIFO (Last In, First Out) access: the most recently added element is the first to be removed.
Think of a stack of plates: you add plates to the top and remove from the top.
push(3) → [1, 2, 3] ← top
push(5) → [1, 2, 3, 5] ← top
pop() → [1, 2, 3] (5 removed, returned)
| Operator | Signature | Description |
|---|---|---|
empty |
→ Stack |
Create empty stack |
push |
Stack × Element → Stack |
Add to top |
pop |
Stack → Stack |
Remove top |
top |
Stack → Element |
Peek at top |
isEmpty |
Stack → Boolean |
Check if empty |
All core operations are $O(1)$.
KEY TAKEAWAY: Whenever a problem requires reversing an order or tracking a nested/recursive structure, consider a Stack.
A queue is a collection with FIFO (First In, First Out) access: the first element added is the first removed.
Think of a queue at a supermarket checkout — customers join at the rear, leave from the front.
enqueue(A) → [A]
enqueue(B) → [A, B]
enqueue(C) → [A, B, C]
dequeue() → [B, C] (A removed, returned)
| Operator | Signature | Description |
|---|---|---|
empty |
→ Queue |
Create empty queue |
enqueue |
Queue × Element → Queue |
Add to rear |
dequeue |
Queue → Queue |
Remove from front |
front |
Queue → Element |
Peek at front |
isEmpty |
Queue → Boolean |
Check if empty |
All core operations are $O(1)$.
EXAM TIP: BFS requires a queue; DFS uses a stack (or recursion). Knowing which traversal uses which ADT is frequently tested.
A priority queue is a collection where each element has an associated priority, and elements with higher priority are removed first (regardless of insertion order).
| Operator | Signature | Description |
|---|---|---|
empty |
→ PriorityQueue |
Create empty priority queue |
insert |
PQueue × Element × Priority → PQueue |
Add with priority |
removeMin |
PQueue → PQueue |
Remove highest-priority element |
min |
PQueue → Element |
Peek at highest-priority element |
isEmpty |
PQueue → Boolean |
Check if empty |
Note: “highest priority” is usually the minimum key in min-priority-queue convention.
| Operation | Complexity |
|---|---|
insert |
$O(\log n)$ |
removeMin |
$O(\log n)$ |
min (peek) |
$O(1)$ |
COMMON MISTAKE: A priority queue is NOT a sorted list. Elements are not maintained in sorted order — only the minimum (or maximum) is guaranteed to be accessible efficiently.
| Property | Stack | Queue | Priority Queue |
|---|---|---|---|
| Access order | LIFO | FIFO | By priority |
insert time |
$O(1)$ | $O(1)$ | $O(\log n)$ |
remove time |
$O(1)$ | $O(1)$ | $O(\log n)$ |
| Key algorithm use | DFS, backtracking | BFS | Prim’s, Dijkstra’s |