Stack

Infix to Postfix

What is Postfix Notation?

In postfix notation, also known as Reverse Polish Notation, you write the operator right after its two operands instead of between them.

So the everyday expression 3 + 4 becomes 3 4 + once converted. There's no ambiguity about order of operations here: the position of each operator in the string already tells you exactly when to apply it, so parentheses become unnecessary.

Note: Higher precedence means the operation will happen first. When operators have equal precedence, they are evaluated left-to-right (except for exponentiation which is right-to-left).

Infix to Postfix Conversion Steps

  1. Initialize an empty stack and an empty output string.
  2. Scan the infix expression from left to right.
  3. If the element is an operand, add it to the output.
  4. If the element is a '(', push it onto the stack.
  5. If the element is a ')', pop from the stack and add to output until '(' is encountered.
  6. If the element is an operator, pop from the stack all operators with higher or equal precedence, then push the current operator.
  7. After scanning, pop all remaining operators from the stack.

How Does It Work?

Take (A + B) * (C - D). The amber token is the one being scanned, the stack holds operators waiting for their operands, and the output grows left to right:

1. Scan '(' '(' is pushed onto the stack

(A+B)*(C-D)stack(output(empty)

2. Scan 'A' 'A' is an operand, so it goes straight to the output

(A+B)*(C-D)stack(outputA

3. Scan '+' Push '+' onto the stack

(A+B)*(C-D)stack(+outputA

4. Scan 'B' 'B' is an operand, so it goes straight to the output

(A+B)*(C-D)stack(+outputAB

5. Scan ')' Pop operators to the output until '(' is found

(A+B)*(C-D)stackemptyoutputAB+

6. Scan '*' Push '*' onto the stack

(A+B)*(C-D)stack*outputAB+

7. Scan '(' '(' is pushed onto the stack

(A+B)*(C-D)stack*(outputAB+

8. Scan 'C' 'C' is an operand, so it goes straight to the output

(A+B)*(C-D)stack*(outputAB+C

9. Scan '-' Push '-' onto the stack

(A+B)*(C-D)stack*(-outputAB+C

10. Scan 'D' 'D' is an operand, so it goes straight to the output

(A+B)*(C-D)stack*(-outputAB+CD

11. Scan ')' Pop operators to the output until '(' is found

(A+B)*(C-D)stack*outputAB+CD-

12. End of scan Scan finished — pop everything left on the stack

(A+B)*(C-D)stackemptyoutputAB+CD-*
Token being scannedOn the stack / not yet scannedWritten to the output

Final postfix: A B + C D - *. Notice the operands never touch the stack — only operators wait there, and each one is released the moment an operator of equal or higher precedence arrives, or its bracket closes.

Operator Precedence Table

OperatorMeaningPrecedence
( )ParenthesesHighest
^ %Exponentiation / Modulus2
* /Multiplication / Division3
+ -Addition / Subtraction4 (Lowest)

Note: Higher precedence means the operation will happen first. When operators have equal precedence, they are evaluated left-to-right (except for exponentiation which is right-to-left).

Visualize the conversion from infix to postfix notation

Conversion Status

Enter an infix expression and click Convert

Stack

Stack is empty

Output

Output will appear here

Test Your Knowledge before moving forward!

Stack Quiz Challenge

How it works:

  • +1 point for each correct answer
  • 0 points for wrong answers
  • -0.5 point penalty for viewing explanations
  • Earn stars based on your final score (max 5 stars)

PostFix implementation using Stack

// Postfix Evaluation using Stack (JavaScript)
function evaluatePostfix(expression) {
  let stack = [];
  
  for (let char of expression) {
    if (!isNaN(char)) {
      stack.push(parseInt(char));
    } else {
      const b = stack.pop();
      const a = stack.pop();
      
      switch(char) {
        case '+': stack.push(a + b); break;
        case '-': stack.push(a - b); break;
        case '*': stack.push(a * b); break;
        case '/': stack.push(Math.floor(a / b)); break;
      }
    }
  }
  return stack.pop();
}

// Example: "23*5+" becomes (2*3)+5 = 11
console.log(evaluatePostfix("23*5+")); // Output: 11

Done With the Learning

Mark Polish : postfix as done and view it on your dashboard

Explore other conversions