Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the convertToPostfix algorithm to the LinkedStack. Here is the code so far for the LinkedStack class: public final class LinkedStack implements StackInterface { private

Implement the convertToPostfix algorithm to the LinkedStack. Here is the code so far for the LinkedStack class:

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 static int getPriority(char c){ switch(c){ case '(': return 0; case '/': case'*': return 2; case'+':case'-':return 1; default: return 999; } } */ 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; } } }

convertToPostfix algorithm:

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

LO1 Summarize the organizations strategic planning process.

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago