Q.Evaluate following postfix expressions while showing status of stack after each operation given A=3, B=5, C=1, D=4
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →We evaluate postfix expressions by scanning left to right, pushing operands onto a stack, and when an operator is encountered, popping the required number of operands, applying the operator, and pushing the result back.
Why Postfix Evaluation Works
Postfix (Reverse Polish Notation) places operators after their operands, which eliminates the need for parentheses and operator precedence rules. The algorithm is beautifully simple: maintain a stack of operands. When you see a value, push it. When you see an operator, pop the right number of operands (two for binary operators), compute, and push the result. At the end, the stack holds exactly one value — the answer.
Let's evaluate both expressions step by step, using the given values: A=3, B=5, C=1, D=4.
(a) A B + C *
This is 3 5 + 1 *. We scan left to right.
| Step | Token | Operation | Stack (top → bottom) |
|---|---|---|---|
| 1 | 3 | Push 3 | 3 |
| 2 | 5 | Push 5 | 5, 3 |
| 3 | + | Pop 5, pop 3 → 3+5=8 → push 8 | 8 |
| 4 | 1 | Push 1 | 1, 8 |
| 5 | * | Pop 1, pop 8 → 8×1=8 → push 8 | 8 |
Final stack: [8]
When popping for a binary operator, the first pop is the right operand and the second pop is the left operand. For commutative operations like + and * the order doesn't matter, but for - and / it absolutely does.
Result of (a): 8
(b) A B * C / D *
This is 3 5 * 1 / 4 *.
| Step | Token | Operation | Stack (top → bottom) | …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.