Algorithm of Infix To Prefix
Algorithm of Infix To Prefix
1. Step 1. Push “)” onto STACK, and add “(“ to end of the A
2. Step 2. Scan A from right to left and repeat step 3 to 6 for each
element of A until the STACK is empty
3. Step 3. If an operand is encountered add it to B
4. Step 4. If a right parenthesis is encountered push it onto STACK
5. Step 5. If an operator is encountered then:
6. a. Repeatedly pop from STACK and add to B each operator (on the top
of STACK) which has same
7. or higher precedence than the operator.
8. b. Add operator to STACK
9. Step 6. If left parenthesis is encontered then
10. a. Repeatedly pop from the STACK and add to B (each operator
on top of stack until a left parenthesis is encounterd)
11. b. Remove the left parenthesis
12. Step 7. Exit
Expression = (A+B^C)*D+E^5
Step 1. Reverse the infix expression.
5^E+D*)C^B+A(
Step 2. Make Every '(' as ')' and every ')' as '('
5^E+D*(C^B+A)
Step 3. Convert expression to postfix form.
A+(B*C-(D/E-F)*G)*H
Expression Stack Output Comment
5^E+D*(C^B+A) Empty - Initial
^E+D*(C^B+A) Empty 5 Print
E+D*(C^B+A) ^ 5 Push
+D*(C^B+A) ^ 5E Push
D*(C^B+A) + 5E^ Pop And Push
*(C^B+A) + 5E^D Print
(C^B+A) +* 5E^D Push
C^B+A) +*( 5E^D Push
^B+A) +*( 5E^DC Print
B+A) +*(^ 5E^DC Push
+A) +*(^ 5E^DCB Print
A) +*(+ 5E^DCB^ Pop And Push
) +*(+ 5E^DCB^A Print
End +* 5E^DCB^A+ Pop Until '('
End Empty 5E^DCB^A+*+ Pop Every element
Result
+*+A^BCD^E5