Question

In: Computer Science

Find is the final result of evaluating the following postfix expression using a stack. Show each...

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 -   * +

Solutions

Expert Solution

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 :)


Related Solutions

Using a stack, write a program that turns a simple infix arithmetic expression into a postfix...
Using a stack, write a program that turns a simple infix arithmetic expression into a postfix expression. For example, 1 + 2 * 3 becomes 2 3 * 1 +. Also, evaluate the expression to ensure the expression is correct.
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW...
GIVEN THE FOLLOWING PSOSTFIX EXPRESSION, SHOW TO USE A STACK TO EVALUATE ITS FINAL VALUE. SHOW STACK CONFIGURATION DETAILS 20 3 18 8 - * 10 + +10 12 - /
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Using STL stack class, implement in C++ a function that converts an infix expression to postfix...
Using STL stack class, implement in C++ a function that converts an infix expression to postfix expression,
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses....
(Convert infix to postfix) Note: Postfix notation is a way of writing expression without using parentheses. For example, the expression ( 11 + 12 ) * 13 would be written as 11 12 + 13 * Assume that ALWAYS there is a space between operands and operators in the input expression. Use two stacks, one to store the operands and one to store the operators. Your program only accpets following operators : ( ) + - / * Write a...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack...
Implement in Python using stack operations. Postfix Calculator Post fix calculator • We use a stack • When an operand is read, push it on statck • When an operator is read i.e +, *. /, - – Pop two from the top of the stack and apply the operator and push the result on stack if there is one value instead of two display an error message • Keep repeating until an equal sign, = is read; pop from...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression...
Using Java 8. Write a program that reads an expression in postfix notation, builds the expression tree and prints the expression in prefix and infix notation and evaluates the expression. (Hint use a stack)
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression...
IN JAVA PLZ follow all directions SHOW OUPUT! Write class PostfixEvaluator that evaluates a postfix expression such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of single digits and operators into a StringBuilder, The program should read the expression and evaluate it (assume it's valid). The algorithm to evaluate a postfix expression is shown below. Use +, -, *, /, and ^. ^ is the exponent. Append a right parenthesis...
Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) *...
Show, step by step, how the stack-based algorithm will transform the expression (1 + 2) * (7 − 2) into a postfix expression, and then how a second stack-based algorithm will compute the value of this postfix expression.
Find an expression, in terms of the initial and final photon wavelengths, for kinetic energy and...
Find an expression, in terms of the initial and final photon wavelengths, for kinetic energy and momentum for the electron after collision.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT