Infix to Prefix Conversion

While we use infix expressions in our day to day lives. Computers have trouble understanding this format because they need to keep in mind rules of operator precedence and also brackets. Prefix and Postfix expressions are easier for a computer to understand and evaluate.
Given two operands a and b and an operator \odot, the infix notation implies that O will be placed in between a and b i.e  a \odot b . When the operator is placed after both operands i.e ab\odot, it is called postfix notation. And when the operator is placed before the operands i.e  \odot a b, the expression in prefix notation.


Algorithm:
Step 1.  Push “)” onto STACK, and add “(“ to end of the A
 Step 2. mScan A from right to left and repeat
step 3  to 6 for each element of A until the STACK is empty Step 3. If an operand is encountered add  it to B
Step 4.  If a right parenthesis is encountered push it onto STACK
 Step 5.   If an operator is encountered then:
 a. Repeatedly pop from STACK and add to B each operator (on the top of STACK) which has same or higher precedence than the operator.
 b. Add operator to STACK
 Step 6.   If left parenthesis is encontered then
a. Repeatedly pop from the STACK and add to B (each operator on top of stack until a left parenthesis is encounterd)
 b. Remove the left parenthesis
Step 7. Exit

Source Code: