Stacks & queuesAnimated
Infix to Postfix Conversion
Shunting-yard: an operator stack orders operators by precedence.
Time
O(n)
Extra space
O(n)
Use it for
Parsing arithmetic expressions.
How it works
- Scan the expression. Operands (letters, numbers) go straight to the output.
- Operators wait on a stack. Before pushing an operator, pop any operators on top with higher or equal precedence into the output (^ is right-associative).
- An opening bracket is pushed; a closing bracket pops operators to the output until the matching opening bracket.
- At the end pop everything left. The output is the postfix expression.
Watch it run
Loading animation…