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
  • (30 Points) Please compute the following postfix expression using stack as shown in your textbook (page...

    (30 Points) Please compute the following postfix expression using stack as shown in your textbook (page 106-107). For every scan, you need to show your stack and indicate the top and bottom of the stack. 3. 3 7+2/2-48* +10+ (30 Points) Please convert the following infix expression to postfix expression using stack as shown in your textbook (page 109-110). For every scan, you need to show your stack and output. Also indicate the top and bottom of the stack. 4.

  • JAVA, please You must write a robust program meaning that your program should not crash with...

    JAVA, please You must write a robust program meaning that your program should not crash with any given data. Data validation must be done any time that user enters an input. Write a program that 1. Gets an infix expression form the user and evaluate the expression using stack ADT a. Finds the postfix equivalent of the given infix expression b. Evaluate the created postfix expression. c. Note: your program should not crash when you enter an invalid expression such...

  • a) Show the steps that a stack uses to convert the algebraic expression a*(b+c/d from infix to postfix notation. Indicate each intermediate change in the stack and postfix output. (Be sure to...

    a) Show the steps that a stack uses to convert the algebraic expression a*(b+c/d from infix to postfix notation. Indicate each intermediate change in the stack and postfix output. (Be sure to identify how operator precedence is determined. b) show the steps a stack uses to evaluate the postfix expression from part (a) when (a-6, b-4, c-2, d 5) c) Show the steps a stack uses to produce an expression tree with the postfix expression from part (a). a) Show...

  • 37+2/2-48+10+ (30 Points) Please convert the following infix expression to postfix expression using stack as shown...

    37+2/2-48+10+ (30 Points) Please convert the following infix expression to postfix expression using stack as shown in your textbook (page 109-110). For every scan, you need to show your stack and output. Also indicate the top and bottom of the stack. 4. 19-7*2+(6+8)/2-5 o C++ code submission over Canvas is necessary. Please submit your solutions to the Canvas on ue date as Word or Pdf file. You can solve the questions on a paper and scan it through mobile app...

  • Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a...

    Using ADT Stack: Evaluating infix expressions by converting them to postfix expressions Postfix notation: In a postfix expression, a binary operation follows its two opperands. The order of the operands in a infix expression is the same as in the corresponding postfix expression but the order of the operators might change based on the precedence of the operators and the existing of paranthses. Infix Postfix a + b a b + (a + b) * c a b + c...

  • Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user....

    Question: Write a Java program for Evaluating Postfix Expression 1. Input a postfix expression from user. 2. Evaluate expression using double stack made of your own linked list. 3. Show the result of expression or error if incorrect. Evaluating Postfix Expression Input an expression: 2 10 + 9 6 - / Evaluating Postfix Expression Input an expression: 20 10 + 9 6 - 1 Evaluating Postfix Expression Input an expression: 2 10 + 9 - / Result = 4.0 Result...

  • Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a...

    Python Issue Postfix notation (also known as Reverse Polish Notation or RPN in short) is a mathematical notation in which operators follow all of its operands. It is different from infix notation in which operators are placed between its operands. The algorithm to evaluate any postfix expression is based on stack and is pretty simple: Initialize empty stack For every token in the postfix expression (scanned from left to right): If the token is an operand (number), push it on...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Evaluate the infix expression (5 + 9) * 4 - (4 ^ 5) using two stacks:...

    Evaluate the infix expression (5 + 9) * 4 - (4 ^ 5) using two stacks: an operator stack and operand stack. Show the contents of both stacks after processing each input character. The top of the stack is the rightmost entry.

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