Stack/QueueStack & Queue
Stack (Bracket Matching)
A last-in, first-out collection where all insertions and removals happen at one end, the top.
a
{
0
[
1
(
2
)
3
(
4
)
5
]
6
}
7
(
8
stack (bottom → top)
1/11Validate bracket nesting. A stack fits because the most recently opened bracket must be the first one closed — last in, first out.
Current characterOpener waiting on stackMatched pairMismatch
PseudocodeLearn Stack →
1stack = []2for ch in s:3 if ch is an opener: stack.push(ch)4 else if stack empty or stack.top does not match ch: return invalid5 else: stack.pop()6return stack emptyVariables
i0
stackSize0
Complexity
access O(n)
search O(n)
insert O(1)
delete O(1)
Speed