Question

In: Computer Science

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 - /

Solutions

Expert Solution

Solution:

Given,

=>Postfix expression = 20 3 18 8 - * 10 + + 10 12 - /

Explanation:

=>To evaluate postifix expression operand stack is used means operands are pushed into the stack.

=>While traversing the expression from left to right if we find an operator then we need to pop off two operands from the top of the stack one by one and need to perform the operation and result is pushed back into the stack.

Initially:

=>Let say stack of size = 5

=>Top is the pointer which keeps track of elements inside the stack. Initially there is no element in the stack so top = -1 when an element is inserted into the stack top pointer is incremented by 1. When an element is deleted from the stack top pointer is decremented by 1.

top = -1

Step 1:

=>Push 20 into the stack.

20

top = 0

Step 2:

=>Push 3 into the stack.

3
20

top = 1

Step 3:

=>Push 18 into the stack.

18
3
20

top = 2

Step 4:

=>Now push 8 into the stack.

8
18
3
20

top = 3

Step 5:

=>Now pop off operands 8 and 18, result = 18 - 8, result = 10 and push back 10 into the stack.

10
3
20

top = 2

Step 6:

=>Now pop off operands 10 and 3 , result = 3*10, result = 30 and push back 30 into the stack.

30
20

top = 1

Stpe 7:

=>Now push 10 into the stack.

10
30
20

top = 2

Step 8:

=>Now pop off 10 and 30, result = 30 + 10, result = 40 and push back 40 into the stack.

40
20

top = 1

Stpe 9:

=>Now pop off operands 40 and 20, result = 20 + 40, result = 60 and push back 60 into the stack.

60

top = 0

Step 10:

=>Now push 10 into the stack.

10
60

top = 1

Step 11:

=>Now push 12 into the stack.

12
10
60

top = 2

Step 12:

=>Now pop off 12 and 10, result = 10 - 12, result = -2 and push back -2 into the stack

-2
60

top = 1

Step 13:

=>Now pop off -2 and 60, result = 60/-2, result = -30 and push back -30 into the stack.

-30

top = 0

Step 14:

=>As whole expression is traversed so final result will be last value present inside the stack hence final result = -30.

I have explained each and every part with the help of statements attached to it.


Related Solutions

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 -   * +
Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4...
Use the algorithm below to evaluate the following infix expression: a) a + 3 * 4 – 9 b) ( 2 + 6 ) / ( 3 – 5 ) . Algorithm WRITE STEP BY STEP Scan the characters in the infix expression. While there are characters left in the infix expression: Check the next character in the expression. 1. If the next character is operand, remove it from the expression and push it onto the operand stack. 2. If...
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.
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab...
Problem 2: Evaluate the following postfix expression, using the rules given in Section I of Lab 10: 1 5 4 – 3 + * 3.   Computer Science
Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if...
Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. (3 * 5 > 10) || (20 < 15) True False Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. "Medium" < "High" True False Evaluate the following logical expression. Choose True if the expression evaluates to true; choose False if the expression evaluates to false. (4...
Use the power series method to solve the given initial-value problem. (Format your final answer as...
Use the power series method to solve the given initial-value problem. (Format your final answer as an elementary function.) (x − 1)y'' − xy' + y = 0, y(0) = −4, y'(0) = 7 y =
Use the power series method to solve the given initial-value problem. (Format your final answer as...
Use the power series method to solve the given initial-value problem. (Format your final answer as an elementary function.) (x − 1)y'' − xy' + y = 0, y(0) = −7, y'(0) = 2
Use the power series method to solve the given initial-value problem. (Format your final answer as...
Use the power series method to solve the given initial-value problem. (Format your final answer as an elementary function.) (x − 1)y'' − xy' + y = 0, y(0) = −7, y'(0) = 2 Use the power series method to solve the given initial-value problem. (Format your final answer as an elementary function.)
Show what the expression E • J represents in field theory, what would its analogous expression...
Show what the expression E • J represents in field theory, what would its analogous expression be in circuit theory?
Evaluate the given expression and express the results using the usual format for writing numbers (instead...
Evaluate the given expression and express the results using the usual format for writing numbers (instead of scientific notation) 32C2=
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT