Advertisements
Advertisements
Question
Evaluate following postfix expression while showing status of stack after the operation given A = 3, B = 5, C = 1, D = 4.
A B + C *
Answer in Brief
Advertisements
Solution
A B + C *
A = 3, B = 5, C = 1
Replacing Literals with Values:
Expression : A B + C *
After Substitution Expression: 3 5 + 1 *
Scanning from left to right
| Symbol | Action | Intermediate Process/Output | Stack, Initially Stack is Empty [ ] |
| 3 | PUSH(3) | 3 | |
| 5 | PUSH(5) | 3 5 | |
| + | POP twice, Evaluate and PUSH Back | POP( ) => 5 POP( ) => 3 3 + 5 = 8 PUSH(8) |
3 [ ] #Empty 8 |
| 1 | PUSH(1) | 8 1 | |
| * | POP twice, Evaluate and PUSH Back | POP( ) => 1 POP( ) => 8 8 * 1 = 8 PUSH(8) |
8 [ ] #Empty 8 |
| End of Expression | POP | [ ] | |
| Answer : 8 |
shaalaa.com
Notations for Arithmetic Expressions
Is there an error in this question or solution?
