Question

Evaluate the postfix expression shown below using a stack. Begin with an empty stack and show...

Evaluate the postfix expression shown below using a stack. Begin with an empty stack and show the contents of the stack after reading each token and indicate where “top” is. After reading all the tokens in the expression, the final result should be on the stack.

5 8 9 + * 7 4 * 5 3 2 * * + *

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Postfix expression evaluation:

In postfix expression evaluation, the stack data structure is used and we need the expression from left to right.

If it is an operand then it is pushed on to the stack and if it is an operator then two operands are popped from the stack and then evaluated. The result is pushed back onto the stack and the same process is repeated until the end of the expression.

The given postfix expression is:

5 8 9 + * 7 4 * 5 3 2 * * + *

The first operand is pushed onto the stack.

5 <- Top

The second operand is pushed onto the stack.

8 <- Top 5

The third operand is pushed onto the stack.

9 <- Top 8 5

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

<- Top 17 5

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

85 <- Top

The operand is pushed onto the stack.

7 <- Top 85

The operand is pushed onto the stack.

4 <- Top 7 85

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

28 <- Top 85

The operand is pushed onto the stack.

5 <- Top 28 85

The operand is pushed onto the stack.

3 <- Top 5 28 85

The operand is pushed onto the stack.

2 <- Top 3 5 28 85

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

6 <- Top 5 28 85

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

<- Top 30 28 85

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

58 <- Top 85

The top two items are popped from the stack and operation is performed and the result is stored onto the stack.

4930 <- Top

The final result is 4930 and it will be the last element in the stack.

Add a comment
Know the answer?
Add Answer to:
Evaluate the postfix expression shown below using a stack. Begin with an empty stack and show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT