Stacks can be used to evaluate an expression in postfix notation. For simplification, we are assuming that operators used in expressions are binary operators. The detailed step by step procedure is given in Algorithm.
Algorithm: Evaluation of postfix expression
Step 1: INPUT postfix expression in a variable, say postExp
Step 2: For each character in postExp, REPEAT Step 3
Step 3: IF character is an operand
THEN PUSH character on the Stack
ELSE POP two elements from the Stack, apply
the operator on the popped elements and PUSH the computed value onto
the Stack
Step 4: IF Stack has a single element
THEN POP the element and OUTPUT as the net result
ELSE OUTPUT “Invaild Postfix expression”
Example
Figure shows the step-by-step process of evaluation of the postfix expression 7 8 2 * 4 / + using Algorithm.
Evaluation of postfix expression 7 8 2 * 4 /+
