In: Computer Science
Find is the final result of evaluating the following postfix expression using a stack. Show each push and pop operation.
85 5 / 4 * 5 6 + 10 5 - * +
Let stack be s
=>85 is an operand, we push it in stack, s =85
=>5 is an operand, we push it in stack, s = 5,85(where5 is the top element)
=>/ is an operator, we pop 5 and 85, s becomes empty, 85/5 = 17 and we push 17 in stack, s = 17
=>4 is an operand, we push it in stack, s = 4,17(where 4 is the top element)
=>* is an operator, we pop 4 and 17, s becomes empty, 17*4 = 68 and we push 68 in stack, s = 68
=>5 is an operand, we push it in stack, s = 5,68(where 5 is the top element)
=>6 is an operand, we push it in stack, s = 6,5,68(where 6 is the top element)
=>+ is an operator, we pop 6 and 5, s now holds only 68, 5+6 = 11 and we push 11 in stack, s = 11,68(where 11 is the top element)
=>10 is an operand, we push it in stack, s = 10,11,68(where 10 is the top element)
=>5 is an operand, we push it in stack, s = 5,10,11,68(where 5 is the top element)
=>- is an operator, we pop 5 and 10, stack becomes s =11,68(where 11 is the top element), 10-5 = 5, we push 5 in the stack, s = 5,11,68
=>* is an operator, we pop 5 and 11, stack becomes s = 68, 11*5 = 55 an we push it in the stack, s = 55,68(where 55 is the top element
=>+ is an operator, we pop 55 and 68, stack becomes empty, 68+55 = 123
Therefore, final result is 123
Please give an upvote if you liked my soution.
Thank you :)