Infix to Postfix

Infix to Postfix

Algorithm:
  • Create a stack to operator
  • Create empty postfix string named postfixStr
  • Iterate expression string from start to end
    • If currentCharacter is operand then,
        >postfixStr += currentCharacter
    • Else If currentCharacter is opening bracket then,
      • stack.push(currentCharacter)
    • Else If currentCharacter is closing bracket then,
      • Pop stack and add popped character to postfixStr till we find opening bracket in stack
    • Else
      • If stack.top operator precedence is higher or equal to currentCharacter precedence then,
        • postfixStr += stack.pop
        • Repeat till previous condition not break
      • stack.push (currentCharacter)
  • Return postfixStr

Latest Source Code:
Github: InfixToPostfix.java


Output:

A + B * C + D   ABC*+D+
(A + B) * (C + D)   AB+CD+*
A * B + C * D   AB*CD*+
A + B + C + D   AB+C+D+
a+b*(c^d-e)^(f+g*h)-i   abcd^e-fgh*+^*+i-

Author: Hrishikesh Mishra