Algorithm for Infix → Postfix Conversion
We scan the infix expression left to right.
1. If character is operand (A, B, 1, 2, …)
👉 Append it directly to output.
2. If character is (
👉 Push it onto the stack.
3. If character is )
👉 Pop from stack and append to output until a ( is found.
👉 Remove the ( from stack (don’t add it to output).
4. If character is an operator (+, -, *, /, ^)
👉 While stack is not empty AND precedence of top of stack ≥ precedence of current
operator:
Pop from stack and append to output.
👉 Push current operator to stack.
5. After scanning full expression
👉 Pop all remaining operators from stack and append to output.
🔹 Precedence Rules
+ and - → precedence 1
* and / → precedence 2
^ (power) → precedence 3 (right associative → handled carefully)
🔹 Example (Quick)
Expression: A+B*C
A → operand → output = A
+ → operator → stack = +
B → operand → output = AB
* → operator → check top = +
precedence(*) > precedence(+) → push *
stack = +, *
C → operand → output = ABC
End → pop stack → output = ABC*+