Question: JAVA need help to implement the algorithm convertToPostfix(infix) into a LinkedStack adt . Please do not copy paste code from previous similar questions since those

JAVA need help to implement the algorithm convertToPostfix(infix) into a LinkedStack adtJAVA need help to implement the algorithm convertToPostfix(infix) into a LinkedStack adt..

Please do not copy paste code from previous similar questions since those did not work or explain what is happening.

import java.util.EmptyStackException;

/** * * @ * @param */ public final class LinkedStack implements StackInterface { private Node topNode; public LinkedStack() { topNode = null; } public void push(T newEntry) { Node newNode = new Node(newEntry, topNode); topNode = newNode; } public T peek() { if(isEmpty()) { throw new EmptyStackException(); } else return topNode.getData(); } public boolean isEmpty() { return topNode == null; } public void clear() { topNode = null; } public T pop() { T top = peek(); assert topNode != null; topNode = topNode.getNextNode(); return top; } private class Node { private T data; private Node next; private Node(T dataPortion) { this(dataPortion,null); } private Node(T dataPortion, Node nextNode) { data = dataPortion; next = nextNode; } private T getData() { return data; } private void setData(T newData) { data = newData; } private Node getNextNode() { return next; } private void setNextNode(Node nextNode) { next = nextNode; } } }

Algorithm convertToPostfix(infix) // Converts an infix expression to an equivalent postfix expression, operatorStack - a new empty stack postfix - a new empty string while (infix has characters left to parse) nextCharacter next nonblank character of infix switch (next character) Case variable: Append nextCharacter to postfix break case 'A operatorStack.push(nextCharacter) break case + case ''; case ''case while (!operatorStack isEmpty and precedence of nextCharacter precedence of operatorstack peek)) Append operatorStack peek) to postfix operatorStack.pop() operatorStack.push(nextCharacter) break case '( operatorStack.push(nextCharacter) break case ')'//stack is not empty if infix expression is valid topOperator - operatorStack.pop while (topOperator != '') Append topOperator to postfix topOperator - operatorStack.pop break default: break while operatorStack.isEmpty()) topOperator = operator Stack.pop Append topOperator to postfix } return postfix

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!