Algorithm
Suppose P is an expression written in postfix notation. This algorithm finds VALUE of postfix expression P.
1) Add ‘)’ to the end of P.
2) Scan P from left to right and repeat step3 and step4 for each symbol of P until sentinel ‘)’ is encountered.
3) If an operand is encountered, push it onto stack
4) If an operator encountered, then
a) Pop two elements from the stack, where A is topmost element and B is next to top element.
b) Evaluate BA.
c) Push above result onto stack.
5) VALUE ←top element on the stack.
6) Exit.
Wednesday, September 9, 2009
Algorithm to evaluate a postfix expression
Author: Dr. Rajesh Rolen
| Posted at: 9:49 PM |
Filed Under:
Data Structure and Algorithm
|

Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment