Skip to content
StackDecode
Stacks & queuesAnimated

Postfix Evaluation

Push numbers; each operator pops two and pushes the result.

Time

O(n)

Extra space

O(n)

Use it for

Calculators and virtual machines.

How it works

  1. In postfix notation the operator comes after its operands, so no brackets are needed.
  2. Read tokens left to right. A number is pushed onto the stack.
  3. An operator pops the top two numbers, applies the operation (second-from-top first), and pushes the result.
  4. When the input ends, the single number left on the stack is the answer.

Watch it run

Loading animation…