Skip to content

Study guides & original practice

Evaluate postfix expressions with a stack

Read a postfix expression from left to right. Push each number onto a stack. At a binary operator, pop the right operand first, then the left operand, calculate left operator right, and push the result. A valid completed expression leaves exactly one value.

By ExamCat · Original study lesson · Published November 24, 2026

Set the rules before tracing

These exercises use space-separated tokens, binary +, −, * and /, and ordinary real-number division. A signed number such as −3 is one operand token. Stacks are written from bottom to top, so the rightmost entry is the next one popped. An empty stack is []. If those operations are unfamiliar, start with stack versus queue practice.

Postfix places an operator after its operands: 9 4 − means 9 − 4. It does not mean 4 − 9. The most recently pushed value is the right operand, even though you remove it first.

Worked trace: 18 6 3 − / 2 +

This expression means (18 / (6 − 3)) + 2. Follow the token order; do not search for multiplication or division before processing earlier operators.

  1. Read 18: push it. Stack [18].
  2. Read 6: stack [18, 6].
  3. Read 3: stack [18, 6, 3].
  4. Read −: pop right = 3, then left = 6. Push 6 − 3 = 3. Stack [18, 3].
  5. Read /: pop right = 3, then left = 18. Push 18 / 3 = 6. Stack [6].
  6. Read 2: stack [6, 2].
  7. Read +: pop 2 and 6. Push 6 + 2 = 8. Stack [8].

Exactly one value remains, so the answer is 8. At each stage, a value on the stack represents a number or a completed subexpression still waiting to be used.

Three checks that catch invalid input

  • Before a binary operator: at least two values must be present. 7 + fails when + tries to obtain its second operand.
  • Before division: the right operand must not be zero. 8 3 3 − / reaches [8, 0] and then attempts division by zero.
  • At the end: exactly one value must remain. 5 2 4 + leaves [5, 6], so it is unfinished rather than an answer of 6.

For expressions using only binary operators, the number of operands must be one more than the number of operators. This is necessary but insufficient: + 5 2 has the right counts but fails immediately because the operator appears before its operands.

Six exercises with answer explanations

Write the stack after every operator before revealing an answer.

1. Evaluate 14 5 − 2 *.

Check the subtraction order

After −, the stack is [9], since 14 − 5 = 9. After pushing 2 and multiplying, it is [18]. The answer is 18. Reversing the first two pops in the calculation would incorrectly produce −18.

2. Evaluate 21 3 / 4 −.

Check the division order

After /, [7]. After −, [3]. You calculated 21 / 3 − 4, not 3 / 21 − 4. The answer is 3.

3. Evaluate 5 2 3 + *.

Keep the earlier operand waiting

After +, [5, 5]. The first 5 was waiting while 2 + 3 was evaluated. Multiplication produces [25]. This is 5 × (2 + 3), not (5 × 2) + 3.

4. Evaluate −3 8 + 2 /.

Keep the signed token intact

Push −3 as one value. After +, [5]; after /, [2.5]. These exercises use real-number division. A programming exercise that specifies integer division may require a different result; state that rule before evaluating.

5. Evaluate 10 4 − 2 −.

Trace two successive subtractions

The first subtraction produces [6]; the second produces [4]. This encodes (10 − 4) − 2. By comparison, 10 4 2 − − first produces [10, 2], then [8], encoding 10 − (4 − 2).

6. Identify the first failure in each: 4 2 + +, 9 2, and 6 1 1 − /.

Distinguish the three failure types

The first expression reaches [6] after the first +, then lacks two operands for the second +. The second reaches the end with two values, so it is incomplete. The third reaches [6, 0], then attempts division by zero. None has a valid numerical answer under our rules.

Turn a wrong answer into a useful review question

If addition works but subtraction fails, record which popped value is left and which is right. If intermediate results disappear, write the full stack instead of only its top. Put a missed expression on a card in the free manual flashcard maker, with the operator-by-operator stack trace on the back.

For the underlying algorithm, see Berea College’s postfix evaluation lesson. A James Madison University data-structures worksheet also asks students to trace stack-based evaluation. The exercises here use independently written expressions and explanations.

Practice your own class material

Generate flashcards, notes and quizzes from your lecture notes in Exam Cat.

Try 7 days free ↗

Yearly plan: 7 days free, then $39.99/year. Cancel before the trial ends to avoid a charge.

APUSH study guide · Biology study guide · APUSH practice · All study tools