Advertisements
Advertisements
Question
Evaluate using stack 10, 3,*,30,2,*,-
Evaluate
Advertisements
Solution
Scan the expression from left to right and use a stack. Push 10 and 3; multiply them to get 30. Then push 30 and 2; multiply them to get 60. Finally, subtract the second popped value from the first popped value: 30 − 60 = −30. Therefore, the final answer is −30.
| Operator/Operand | Stack | Calculation | Meaning |
|---|---|---|---|
| 10 | 10 | Operand pushed to stack | |
| 3 | 10, 3 | Operand pushed to stack | |
| * | 30 | 10 × 3 = 30 | Pop last two operands, perform the operation and push the result onto stack |
| 30 | 30, 30 | Operand pushed to stack | |
| 2 | 30, 30, 2 | Operand pushed to stack | |
| * | 30, 60 | 30 × 2 = 60 | Pop last two operands, perform the operation and push the result onto stack |
| - | -30 | 30 − 60 = -30 | Pop last two operands, perform the operation and push the result onto stack |
shaalaa.com
Is there an error in this question or solution?
