3.2.6 · D3Linear Data Structures

Worked examples — Stack applications — balanced parentheses, infix-to-postfix, function call stack

3,110 words14 min readBack to topic

This page is the exhaustive drill for the parent note Stack applications. We will not re-teach the ideas from zero — the parent did that. Instead we hunt down every awkward case the three algorithms can throw at you, so no exam or interview surprises you.

Before we start, one reminder in plain words, because we lean on it in every example:


The scenario matrix

Each algorithm has "obvious" cases and "sneaky" cases. Here is the full grid of what can go wrong. Every cell gets covered by a worked example below (the Ex# tells you which).

# Algorithm Case class What makes it tricky Covered by
A Balanced brackets Empty string (degenerate) Zero characters — is nothing balanced? Ex1
B Balanced brackets Closer before any opener Stack empty when we try to pop Ex1
C Balanced brackets Leftover openers Counts look fine but pile isn't empty Ex2
D Balanced brackets Crossing / wrong type Top of stack ≠ matching opener Ex2
E Balanced brackets Deep valid nesting (limiting) Many pushes, then all pops Ex3
F Infix→Postfix Left-associative equal precedence Must pop the waiting operator Ex4
G Infix→Postfix Right-associative ^ Must NOT pop the equal operator Ex5
H Infix→Postfix Nested parentheses + mixed ops Flush-to-( inside a bigger expression Ex6
I Infix→Postfix Word problem (real world) Translate English → infix → postfix → number Ex7
J Call stack Recursion unwinding (limiting depth) Frames return in reverse order Ex8
K Call stack Exam twist: max stack depth Count peak frames, not total calls Ex9

Ex1 — Empty string & a lone closer (cells A, B)

The picture below shows the two piles side by side — one ends empty (green thumbs-up in your mind), one hits the red "empty-pile pop."

Figure — Stack applications — balanced parentheses, infix-to-postfix, function call stack

Ex2 — Leftover openers & a crossing (cells C, D)


Ex3 — Deep valid nesting, limiting behaviour (cell E)


Ex4 — Left-associative equal precedence (cell F)


Ex5 — Right-associative ^ (cell G)


Ex6 — Nested parentheses + mixed operators (cell H)


Ex7 — Real-world word problem (cell I)


Ex8 — Recursion unwinding (cell J)


Ex9 — Exam twist: peak stack depth (cell K)


Recall Quick self-test across all cells

Empty string "" — balanced? ::: Yes — no unfulfilled promises, stack ends empty. Why does ([)] fail even with matched counts? ::: The closer ) pops [, which does not match — order/nesting is wrong. Postfix of a - b + c? ::: a b - c + (pop the equal-precedence left-assoc -). Postfix of 2 ^ 3 ^ 2? ::: 2 3 2 ^ ^ (right-assoc, do NOT pop the equal ^). Postfix of a * (b + c * d) - e? ::: a b c d * + * e -. Peak frames when main→f→(g;g) with g a leaf? ::: 3 (sibling g calls are sequential, not nested).


Connections

  • Stack — ADT and Implementation — the push/pop/peek used in every trace above.
  • Operator Precedence and Associativity — the rule that Ex4 and Ex5 hinge on.
  • Postfix Evaluation Algorithm — how the "Verify" steps re-run the RPN with one stack.
  • Recursion and the Call Stack — Ex8's frame unwinding in general.
  • Expression Trees and Evaluation — an alternative view of the same precedence structure.
  • Queues — contrast: FIFO would break every LIFO argument here.