An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...

90.2K

Verified Solution

Question

Programming

An arithmetic expression can be represented in three differentformats: infix, prefix, and postfix. In the infix notation, theoperator comes between two operands. In the postfix notation, theoperator comes after its two operands. For example, an expressiona/b can be transformed to ab/. The following are some examples ofthe postfix expressions:

2+6*4 Its postfix notation is 2 6 4 * +

2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -

For this assignment, write a JAVA program that reads an postfixexpression and evaluates the postfix expression.Your program should display the following menu repeatedly:

1. Read an expression in postfix notation.

// Read an infix expression and print out the string forverification.

2. Evaluate the postfix expression.

0. Exit.

For the input string, you can make the followingassumptions:

Input strings are error-free

+, -, *, / are the only operators used

All operand are positive integers consisting of one or moredigits.

Space appears in the input string to separate the operands andoperators.

Implement a class StackLink, which is a stackimplemented by a linked list (Do not use the JAVApredefined class java.util.LinkedList). Your program should usestack operations to evaluate the postfixexpression. The following figure shows how stacks can help.

The following are some results when your run the program:

C:'>java Homework4

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

1

Enter a postfix expression: 32 15 2 * + 36 4 / -

The entered infix expression is: 32 15 2 * + 36 4 / -

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

2

32 15 2 * + 36 4 / - = 53

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

1

Enter a postfix expression: 3 4 / 15 * 9 / 6 8 * + 7 - 54 +

The entered infix expression is: 3 4 / 15 * 9 / 6 8 * + 7 - 54+

Select from:

1. Read an expression in postfix notation.

2. Evaluate the postfix expression

0. Exit.

2

3 4 / 15 * 9 / 6 8 * + 7 - 54 + = 95

Select from:

1. Read an expression in infix notation.

2. Convert infix to postfix.

3. Evaluate the postfix expression

0. Exit.

0

Goodbye.

Answer & Explanation Solved by verified expert
3.9 Ratings (711 Votes)
As per your requirement I have created two classes StackLink and Test StackList internally using user defined linked list Class 1 StackLink class StackLink    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students