INFIX TO POSTFIX CONVERSION

Lets see the precedence of the operators that will be used to convert the given infix to postfix expression and understand the logic for conversion.

Precedence

Precedence
Operator Stack precedence Input precedence
+ - 2 1
/ * 4 3
^ 6 7
( 0 9
) - 0

Logic for conversion

Do refer the code available the end of this section to understand the following theory.

  1. Create an operator stack using arrays of type character.
  2. Scan the infix expression from left to right.
  3. If the character read is an operand then add it to the postfix expression.
  4. Else loop until the stack is not empty and stack precedence of top is more than the input precedence.
    1. If the input character is ')' then pop the top of the stack and add it to the postfix expression until the top of stack is not '('.
    2. Once again pop the top of stack because it will obviously be '(' and break the loop(step 4).
    3. Else if the input character is not ')' then pop the top of stack and add to it the postfix expression.
  5. If the step 4 condition fails and input character is not ')' then push the operator into the stack.
  6. After scanning all the characters of the infix expression, pop the top of operand stack and add it to the postfix expression untill the stack is not empty.
  7. Return the postfix expression.

Code file

Please open this in your pc or with a compatible app in your mobile.

C++ Implementation for INFIX TO POSTFIX CONVERSION

That's it from this blog post. If you liked it then do share this blog with your friends or people who wanna get into programming world. Thank You!

Copyright © NStF Blogs 2021