Stack Applications

The stack is a quiet workhorse. Once you start looking for LIFO patterns, you’ll find them everywhere — in compilers, browsers, recursion, expression parsers, and undo systems.

Here are the most important places stacks earn their keep.

1. Function calls (the call stack)

Every time your program calls a function, the runtime pushes a stack frame containing:

  • The function’s local variables
  • The arguments passed in
  • The return address (where to jump back to)

When the function returns, that frame is popped, restoring the caller’s state.

main() calls foo()      →  push foo's frame
foo() calls bar()       →  push bar's frame
bar() returns           →  pop bar's frame
foo() returns           →  pop foo's frame
back in main()
⚠️

Infinite recursion → infinite pushes → Stack Overflow. That’s the same word, the same data structure, and the same error you’ve seen in your console. Yes, that’s where the website got its name.

2. Balanced parentheses

Given a string of brackets like "([]{})", are they all matched and properly nested?

The trick: scan left to right. Push every opening bracket. When you hit a closing bracket, pop and check it matches. If anything mismatches — or you try to pop an empty stack — it’s invalid. At the end, the stack should be empty.

Validating "([])"
Step 0 / 4(idle)
── top ──
(empty)
size: 0 / 6

We see (, push it. See [, push it. See ] — pop, top was [, matches. See ) — pop, top was (, matches. Stack empty at the end ✓.

3. Undo / Redo

Editors keep two stacks:

  • Undo stack — every action you take gets pushed.
  • Redo stack — when you undo, the popped action goes onto this one.

Press Ctrl+Z: pop from undo, push onto redo. Press Ctrl+Y: pop from redo, push onto undo. A new action clears the redo stack — because once you’ve changed the past, the future branches differently.

4. Expression evaluation

Stacks turn the headache of operator precedence into a clean algorithm.

Infix → Postfix (Shunting Yard)

Humans write 2 + 3 * 4 (infix). Computers love 2 3 4 * + (postfix / RPN) because there are no parentheses and no precedence to worry about — just left to right.

To convert, use one stack for operators:

  • Operand? Append to output.
  • Operator? Pop operators with higher-or-equal precedence to output, then push the new one.
  • ( → push. ) → pop until matching (.

Evaluating Postfix

Once it’s postfix, one stack does everything:

  • Number? Push it.
  • Operator? Pop two operands, apply, push the result.

5. Browser navigation

Two stacks again — one for back, one for forward.

  • Visit a page → push current onto back, clear forward.
  • Click back → pop from back into forward; show the popped one.
  • Click forward → pop from forward into back; show the popped one.

Tree and graph DFS is literally a stack algorithm. The recursive version uses the program’s call stack; the iterative version uses an explicit one.

def dfs(graph, start):
    stack = [start]
    visited = set()
    while stack:
        node = stack.pop()
        if node in visited:
            continue
        visited.add(node)
        for neighbor in graph[node]:
            if neighbor not in visited:
                stack.append(neighbor)

We’ll cover DFS properly on Day 10 — but file it away that “go as deep as possible, then back up” is the LIFO mindset.

7. Backtracking

Solve a problem by trying a choice, exploring deeper, and undoing when it doesn’t work. The undo step is a pop. Sudoku solvers, N-queens, maze solvers — all stack-driven, even when you don’t see one explicitly.

The pattern that ties them all together

If a problem involves:

  • Matched pairs that must be in order (brackets, HTML tags)
  • Reversing something while processing it
  • “Most recent” unfinished thing (next greater element, daily temperatures, histogram)
  • Going deep, then backtracking (DFS, recursion-without-recursion)
  • Operator precedence or expression structure

…then think stack. It’s almost always the right tool.

A common interview hint: if you find yourself wishing you could “look at the last unmatched X,” you want a stack.

Ready to put this into practice? The practice questions are next.