Question
Implement a postfix-to-infix translator using stacks. Postfix notation1[1]is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no
Implement a postfix-to-infix translator using stacks. Postfix notation1[1]is a notation for writing arithmetic expressions in which the operands appear before their operators. There are no precedence rules to learn, and parentheses are never needed. Because of this simplicity, some popular hand-held calculators use postfix notation to avoid the complications of the multiple parentheses required in nontrivial infix expressions. We have discussed how these postfix expressions can be evaluated using a stack in class. You are then to write a Java program to translate a postfix notation to infix notation. A sample output is shown in Figure 2, and some postfix expressions are given in as below for testing purposes. Note that different postfix notation will be used for grading. It is required that the LinkedStack class in part 1should be utilized in solving this problem.
LinkedStack Class:
public class LinkedStack implements StackADT{ //variables private int count; private LinearNode top; //constructors LinkedStack(){ count = 0; top = null; } @Override public void push(T element) { LinearNode newNode = new LinearNode(); //creates new node for the given element newNode.setElement(element); newNode.setNext(null); if(count == 0){ top = newNode; count++; } else{ newNode.setNext(top); top = newNode; count++; } } @Override public T pop() { T result = top.getElement(); top = top.getNext(); count--; return result; } @Override public T peek() { return null; } @Override public boolean isEmpty() { return false; } @Override public int size() { return 0; } public String toString(){ String result = "Linked Stack ["; LinearNode current = top; while(current != null){ result = result + current.getElement() + ", "; current = current.getNext(); } return result; } }
((ANSWER IN JAVA))
No imports
run: Enter a postfix expression: 3 4 + 2 * In Infix Notation: ((3 - 4) * 2) Translate another expression (Y/N)? Y Enter a postfix expression: 4 2 + 351 - * - In Infix Notation: ((4 - 2) - (3 (5 - 1))) Translate another expression (Y/N]?! Figure 2: postfix-to-infix translator Some Postfix For Testing 4 5 7 2 + -*-16- 34 + 2*72+ 57 + 6 2 - * 48 - 42 351 - +* + 18 * 42 + 351-* + 18/Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started