In: Computer Science
java
Infix Expression Stack Postfix Expression
----------------------------------------------------------------------------------------------------------------------------
8 - 9*(2 + 1/4) + 3*7 (empty) (blank)
Postfix Stack Result
--------------------------------------- -------------------- --------------
2 7 + 12 4 / * 8 5 + -
Infix to Postfix expression algorithm
Let, X is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression Y.
Postfix Evaluation
algorithm
1) Add ) to postfix expression.
2) Read postfix expression Left to Right until )
encountered
3) If operand is encountered, push it onto
Stack
[End If]
4) If operator is encountered, Pop two
elements
i) A -> Top element
ii) B-> Next to Top element
iii) Evaluate B operator A
push B operator A onto Stack
5) Set result = pop
6) END