3.2.6 · D1Linear Data Structures

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

1,993 words9 min readBack to topic

This page assumes you have seen nothing. Before you meet a single algorithm in the parent topic, every word, symbol, and picture it leans on is built here, one on top of the next.


0. What even is a "data structure"?

Think of the difference between a queue at a shop (first person in line served first) and a pile of books on your desk (you grab the top one). Same items, opposite access rule. The topic ahead lives entirely inside the "pile of books" rule.

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

The picture above is the whole subject in one image: a vertical pile. You add to the top, you remove from the top. Nowhere else.


1. LIFO — the beating heart

The parent note opens with the phrase LIFO. Let's earn every letter.

The picture: imagine dropping plates onto a spring-loaded dispenser. The plate you drop last sits on top, so it is the plate you take first. The very first plate is buried at the bottom and leaves last.


2. The stack's two moves: push and pop

The algorithms are written using two verbs, push and pop. If those are new, everything else is unreadable — so here they are from zero.

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

Look at the figure: the left board pushes C — it lands above B. The right board popsC leaves, revealing B again. Notice we never reach A at the bottom until everything above is gone. That "buried" feeling is LIFO.

Everything above lives in one prerequisite note: Stack — ADT and Implementation. ADT means Abstract Data Type — a promise about what operations do (push/pop/empty) without caring how they're built inside.


3. Brackets — openers, closers, matching, nesting

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

The top row of the figure shows proper nesting as boxes cleanly inside boxes. The bottom row shows crossing — the [ box and ( box slice through each other, which no physical container can do. That impossibility is exactly why ([)] is called unbalanced, and why a mere count of brackets can't detect it: counting ignores this box-inside-box order.


4. Infix, postfix, and why operators "wait"

The parent's second application converts between two ways of writing math. Both are just word orders for the same calculation.

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

The figure shows the same expression a + b in both layouts. In infix the + sits in the gap; in postfix it waits at the end, after it has both its operands lined up.

You'll meet these ranking rules in full detail in Operator Precedence and Associativity, and you'll use the postfix result in Postfix Evaluation Algorithm and Expression Trees and Evaluation.


5. Functions, calls, returns, and frames


6. FIFO — the opposite, so you know the contrast

The parent contrasts stacks with queues to sharpen your intuition: nesting needs reverse order (LIFO), a fair line needs arrival order (FIFO). See Queues for the full picture.

Recall One-line contrast to lock it in

Stack vs Queue in one sentence? ::: A stack reverses order (last in, first out); a queue preserves order (first in, first out).


Prerequisite map

application 1

application 2

application 3

Data structure idea

LIFO rule

Push and pop and top

Empty stack check

Openers closers nesting

Infix and postfix

Precedence and associativity

Function call and return

Stack frame

FIFO queue contrast

Stack applications

Each foundation feeds directly into one of the parent's three applications: brackets, infix-to-postfix, and the call stack.


Equipment checklist

State the LIFO rule in five words.
Last in, first out.
What does push do to a stack?
Places a new item on the top only.
What does pop do, and what does it return?
Removes and returns the top item only.
In a stack table column, which end is the top?
The rightmost (most recently written) symbol.
What error happens if you pop an empty stack?
There is no top to return — it signals "closing something never opened."
Why can't ([)] exist as real boxes?
The boxes would have to cross edges, which nested containers cannot do.
Where does the operator go in postfix vs infix?
Postfix: after its operands. Infix: between them.
What is precedence?
The strength ranking deciding which operator acts on its operands first.
What breaks ties between equal-precedence operators?
Associativity (left-to-right or right-to-left grouping).
What three things does a stack frame store (at minimum)?
Return address, parameters, local variables.
Why does the last-called function return first?
A caller is frozen until its callee finishes — pure LIFO unwinding.
How is a queue (FIFO) different from a stack (LIFO)?
A queue keeps arrival order; a stack reverses it.

Connections

  • Stack — ADT and Implementation — where push, pop, top, and empty are formally defined.
  • Recursion and the Call Stack — the call-stack foundation in action.
  • Operator Precedence and Associativity — the ranking rules for infix-to-postfix.
  • Postfix Evaluation Algorithm — what you do with the postfix output.
  • Expression Trees and Evaluation — another view of the same expression structure.
  • Queues — the FIFO contrast that sharpens LIFO.