Using the following postfix expression use a stack to solve

Using the following postfix expression, use a stack to solve them producing the final result. Please show use of stack in each case:

11 5 7 5 6 * + 8 - 12 45 % 3 7 11 - + + * / +

Solution

Solution:

Stack Operations              Output

push(11);                             11

push(5);                              11 5

push(7);                               11 5 7

push(5);                               11 5 7 5

push(6);                               11 5 7 5 6

push(pop(5) * pop(6))          11 5 7 30

push(pop(7) + pop(30))        11 5 37

push(8)                                11 5 37 8

push(pop(37) - pop(8))        11 5 29

push(12)                              11 5 29 12

push(45)                              11 5 29 12 45

push(pop(12) % pop(45))    11 5 29 12

push(3)                                 11 5 29 12 3

push(7)                                  11 5 29 12 3 7

push(11)                                11 5 29 12 3 7 11

push(pop(7) - pop(11))          11 5 29 12 3 -4

push(pop(3) + pop(-4))           11 5 29 12 -1

push(pop(-1) + pop(12))         11 5 29 11

push(pop(29) * pop(11))         11 5 319

push(pop(5) / pop(319))         11 0.0156

push(pop(11) + pop(0.0156))    11.0156         

the result of the postfix expression is 11   

Using the following postfix expression, use a stack to solve them producing the final result. Please show use of stack in each case: 11 5 7 5 6 * + 8 - 12 45 %

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site