Evaluate Postfix Expression Using Stack
Evaluating a postfix expression (also called Reverse Polish Notation) is a simple and efficient way to calculate mathematical expressions. Unlike regular (infix) expressions that use parentheses and operator precedence, postfix expressions are written in a way that eliminates the need for such rules.
The stack-based method is the easiest way to evaluate a postfix expression. In this method
- Numbers (operands) are pushed onto a stack.
- When an operator is encountered, the top two numbers are popped from the stack, the operator is applied, and the result is pushed back onto the stack.
At the end, the stack contains the final answer. This method is widely used in calculators, compilers, and programming tasks because it is fast and easy to implement.
In the previous lecture, we used infix expression 7 + 5 * 3 / 5 ^ 1 + (3 - 2)
to convert it into a postfix expression 7 5 3 * 5 1 ^ / +3 2 - +. But in this lecture we will evaluate this postfix expression to final answer using stack.
Algorithm to Evaluate a Postfix Expression Using a Stack
Follow these steps to evaluate a given postfix expression:
Step 01: Scan Each Symbol in the Expression (Left to Right) do the following:
- If the symbol is an operand: Push it onto the stack.
- If the symbol is an operator: Pop the top two operands from the stack. Apply the operator to these two operands and Push the result back onto the stack.
Step 02: After Scanning All Symbols:
- The stack will contain a single value, which is the final result. Pop this value from the stack and display it as the result of the postfix expression.
Evaluate Postfix Expression: 7 5 3 * 5 1 ^ / + 3 2 -+
We will evaluate the given postfix expression step by step using a stack-based approach. At each scan point, we will explain the operation and update the stack. Following diagram will fully explain that how to evaluate Postfix Expression (7 5 3 * 5 1 ^ / + 3 2 -+)
. Let start
Above diagram is fully explained below
Initial Setup
- Expression:
7 5 3 * 5 1 ^ / + 3 2 -+
- Stack: Start with an empty stack.
Step 01: First we scan each element
Scan 7
:
- Explanation:
7
is an operand. Push it onto the stack. - Stack:
[7]
Scan 5
:
- Explanation:
5
is an operand. Push it onto the stack. - Stack:
[7, 5]
Scan 3
:
- Explanation:
3
is an operand. Push it onto the stack. - Stack:
[7, 5, 3]
Scan *
:
- Explanation:
*
is an operator. Pop the top two operands (5
and3
) from the stack. Compute5 * 3 = 15
, then push the result back onto the stack. - Stack:
[7, 15]
Scan 5
:
- Explanation:
5
is an operand. Push it onto the stack. - Stack:
[7, 15, 5]
Scan 1
:
- Explanation:
1
is an operand. Push it onto the stack. - Stack:
[7, 15, 5, 1]
Scan ^
:
- Explanation:
^
is an operator. Pop the top two operands (5
and1
) from the stack. Compute5 ^ 1 = 5
(exponentiation), then push the result back onto the stack. - Stack:
[7, 15, 5]
Scan /
:
- Explanation:
/
is an operator. Pop the top two operands (15
and5
) from the stack. Compute15 / 5 = 3
, then push the result back onto the stack. - Stack:
[7, 3]
Scan +
:
- Explanation:
+
is an operator. Pop the top two operands (7
and3
) from the stack. Compute7 + 3 = 10
, then push the result back onto the stack. - Stack:
[10]
Scan 3
:
- Explanation:
3
is an operand. Push it onto the stack. - Stack:
[10, 3]
Scan 2
:
- Explanation:
2
is an operand. Push it onto the stack. - Stack:
[10, 3, 2]
Scan -
:
- Explanation:
-
is an operator. Pop the top two operands (3
and2
) from the stack. Compute3 - 2 = 1
, then push the result back onto the stack. - Stack:
[10, 1]
Scan +:
- Explanation: + is an operator. Pop the top two operands (10 and 1) from the stack. Compute
10 + 1 = 11
, then push the result back onto the stack. - Stack:
[11]
Final Step
- Explanation: After scanning all symbols of postfix expression
7 5 3 * 5 1 ^ / + 3 2 -+
, the stack contains one value:11
. This is the final result. - Stack:
[11]