Algorithm for evaluate a postfix expression

 Algorithm for evaluate a postfix expression

Algorithm

1. Add a right parenthesis ")" at end of P. [This acts as a sentine1.]

2. Scan P from left to right and repeat steps 3 to 4 for each element of P until the sentine ")" is encountered.

3. If an operand is encountered, put it on Stack.

5. If an operator ⛒ is encountered, then:

    (a) Remove the two top elements of Stack, where A is the top element and B is the next-to-top element.

    (b) Evaluate B ⛒ A.

    (c) Place the result of (b) back on Stack.

         [End of If structure.]

     [End of Step 2 loop.]

6. Set Value equal to the top element on Stack.

7. Exit

Post a Comment

0 Comments