DEV Community

Cover image for Infix to Prefix Conversion USING Stack
hebaShakeel
hebaShakeel

Posted on

Infix to Prefix Conversion USING Stack

Before starting your conversion, make sure you reverse your infix expression and then follow the below mentioned steps.

1)Print the operands as they arrive.

2) If stack is empty or contains a left parentheses on top, push the incoming operator onto the stack.

3) If incoming symbol is '(', push it onto stack.

4) If incoming symbol is ')', pop the stack and print the operators until the left parentheses is found.

5) If the incoming symbol has higher precedence than the top of the stack, push it on the stack.

6) If the incoming symbol has lower precedence than the top of the stack, pop and print the top. Then test the incoming operator against the new top of the stack.

7) If the incoming operator has equal precedence with the top of the operator , use Associativity rule.

8)If associativity is Left -> Right (^), then push the incoming operator.

9)If the associativity is Right -> Left (+,-,*,/) , then pop and print the top of the stack and then push the incoming operator.

10) At the end of the expression, pop and print all operators of stack.

Now reverse back the obtained expression. Your conversion should be ready.
Thank You!

Top comments (0)