Practice Discrete Math

Algorithms / Stack Expression Evaluation

Least You Need to Know: Stacks, Postfix Evaluation, and Expression State

Stacks evaluate postfix expressions because operands arrive before the operator that combines them. When the operator appears, the most recent operands are popped, combined, and the result is pushed back for later use.

The least you need to know

Key notation

postfix operator comes after its operands
push operand store a numeric value for later combination
pop right then left preserve operand order for noncommutative operators

Tiny worked example

  • For `2 3 + 4 *`, push 2, push 3, then `+` pops 3 and 2 and pushes 5.
  • Then push 4.
  • `*` pops 4 and 5 and pushes 20.
  • The final stack value is the expression result.

Common mistakes

How to recognize this kind of problem

Start practice