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
- In postfix notation the operator comes after its operands, so no brackets are needed.
- Read tokens left to right. A number is pushed onto the stack.
- An operator pops the top two numbers, applies the operation (second-from-top first), and pushes the result.
- When the input ends, the single number left on the stack is the answer.
Watch it run
Loading animation…