Infix to Postfix Converter

Infix:
Postfix:

Step by step output for "" expression

Scanned Characters Output Stack Operator Stack

Algorithm to convert infix expression into a postfix expression.

  • Step1: Scan the infix expression from left to right and assign ')' at the end.
  • Step2: If the scanned character is an operand, output it.
  • Step3: Else
  • Step3.1: If the precedence of the scanned character(operator) is greater than the precedence of the operator in the stack (or the stack is empty or the stack contains a '('), push it.
  • Step3.2: Else pop all the operators from the stack, which are greater than or equal to in precedence than that of the scanned operator. After doing that push the scanned operator to the stack. (If you encountered parenthesis while popping then stop there and push the scanned operator in the stack).
  • Step4: If the scanned character is an '(', push it to the stack.
  • Step5: If the scanned character is an ')', pop the stack and output it until a '(' is encountered and discard both the parenthesis.
  • Step6: Repeat Step 2-6 until infix expression is scanned.
  • Step7: Print the output.
  • Step8: Pop and output from the stack until it is not empty.

C program to convert infix expression into a postfix expression.