I am recieving some error I need HELP with I need to implement the LinkedStackCl
ID: 3695307 • Letter: I
Question
I am recieving some error I need HELP with I need to implement the LinkedStackClass In InToPost.java *I Cannot Use The BUILT IN JAVA STACK*
public class StringElement
//This Class works with Strings - it can easily be changed to work with any other Datatype.
{
String data; //Data Field of Stack Node
StringElement link; //Link to next item on Stack
//default constructor
public StringElement()
{
data = "";
link = null;
}
//constructor with a parameter
public StringElement(String x)
{
data = x;
link = null;
}
//Method to set the value of the instance variable.
//Postcondition: data = x;
public void setString(String x)
{
data = x;
}
//Method to return the value of the instance variable.
//Postcondition: The value of data is returned.
public String getString()
{
return data;
}
public String toString()
{
return String.valueOf(data);
}
}
public class LinkedStackClass
{
//reference variable to the top element of the stack
private StringElement stackTop = new StringElement( );
//default constructor
//Postcondition: stackTop = null
public LinkedStackClass()
{
stackTop = null;
}
//Method to initialize the stack to an empty state.
//Postcondition: stackTop = null
public void initializeStack()
{
stackTop = null;
}// end initializeStack
//Method to determine whether the stack is empty.
//Postcondition: Returns true if the stack is empty;
// otherwise, returns false.
public boolean isEmptyStack()
{
return(stackTop == null);
}
//Method to determine whether the stack is full.
//Postcondition: Returns true if the stack is full;
// otherwise, returns false.
public boolean isFullStack()
{
return false;
}
//Method to add newItem to the stack.
//Postcondition: The stack is changed and newItem
// is added to the top of stack.
public void push(String newElement)throws StackOverflowException
{
//create the new node
StringElement newNode = new StringElement( );
newNode.setString(newElement); //push element on stack
//newNode points to next element in stack
newNode.link = stackTop;
//set stackTop to point to the top element
stackTop = newNode;
} //end push
//Method to return the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: If the stack is empty, the method throws
// StackUnderflowException; otherwise, a
// reference to a copy of the top element
// of the stack is returned.
public String peek() throws StackUnderflowException
{
if(stackTop == null)
throw new StackUnderflowException();
return stackTop.getString();
}//end top
//Method to remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The stack is changed and the top
// element is removed from the stack.
// If the stack is empty, the method throws
// StackUnderflowException
public void pop()throws StackUnderflowException
{
if(stackTop == null)
throw new StackUnderflowException();
stackTop = stackTop.link; //advance stackTop to the
//next node
}//end pop
}//end class
public class StackUnderflowException extends StackException
{
public StackUnderflowException()
{
super("Stack Underflow");
}
public StackUnderflowException(String msg)
{
super(msg);
}
}
public class StackException extends RuntimeException
{
public StackException()
{
}
public StackException(String msg)
{
super(msg);
}
}
public class StackOverflowException extends StackException
{
public StackOverflowException()
{
super("Stack Overflow");
}
public StackOverflowException(String msg)
{
super(msg);
}
}
public class InToPost
{
LinkedStackClass theStack;
private String input;
private String output = "";
public InToPost(String in)
{
input = in;
int stackSize = input.length();
}
public String doTrans()
{
for (int j = 0; j < input.length(); j++)
{
char ch = input.charAt(j);
switch (ch)
{
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while(!theStack.isEmptyStack())
{
output = output + theStack.pop();
}
System.out.println(output);
return output;
}
public void gotOper(char opThis, int prec1)
{
while (!theStack.isEmptyStack())
{
char opTop = (char)theStack.pop();
if(opTop == '(')
{
theStack.push(opTop);
break;
}
else
{
int prec2;
if(opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if(prec2 < prec1)
{
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch)
{
while(!theStack.isEmptyStack())
{
char chx = (char)theStack.pop();
if(chx == '(')
break;
else
output = output + chx;
}
}
}
import java.io.*;
import java.util.*;
public class Driver
{
public static void main(String[] args) throws IOException
{
FileReader file = new FileReader("infix.txt");
Scanner scan = new Scanner(file);
String input = scan.nextLine();
String output;
while(scan.hasNextLine())
{
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + ' ');
}
}
}
Explanation / Answer
Hello there,
I am updating the answer.
Converted your StringElement to CharElement !
=====
public class CharElement
{
char data; //Data Field of Stack Node
CharElement link; //Link to next item on Stack
//default constructor
public CharElement()
{
data = '';
link = null;
}
//constructor with a parameter
public CharElement(char x)
{
data = x;
link = null;
}
//Method to set the value of the instance variable.
//Postcondition: data = x;
public void setString(char x)
{
data = x;
}
//Method to return the value of the instance variable.
//Postcondition: The value of data is returned.
public String getString()
{
return String.valueOf(data);
}
public String toString()
{
return String.valueOf(data);
}
}
=====
public class LinkedStackClass {
// reference variable to the top element of the stack
private CharElement stackTop = new CharElement();
// default constructor
// Postcondition: stackTop = null
public LinkedStackClass() {
stackTop = null;
}
// Method to initialize the stack to an empty state.
// Postcondition: stackTop = null
public void initializeStack() {
stackTop = null;
}// end initializeStack
// Method to determine whether the stack is empty.
// Postcondition: Returns true if the stack is empty;
// otherwise, returns false.
public boolean isEmptyStack() {
return (stackTop == null);
}
// Method to determine whether the stack is full.
// Postcondition: Returns true if the stack is full;
// otherwise, returns false.
public boolean isFullStack() {
return false;
}
// Method to add newItem to the stack.
// Postcondition: The stack is changed and newItem
// is added to the top of stack.
public void push(char newElement) throws StackOverflowException {
// create the new node
CharElement newNode = new CharElement();
newNode.setString(newElement); // push element on stack
// newNode points to next element in stack
newNode.link = stackTop;
// set stackTop to point to the top element
stackTop = newNode;
} // end push
// Method to return the top element of the stack.
// Precondition: The stack exists and is not empty.
// Postcondition: If the stack is empty, the method throws
// StackUnderflowException; otherwise, a
// reference to a copy of the top element
// of the stack is returned.
public String peek() throws StackUnderflowException {
if (stackTop == null)
throw new StackUnderflowException();
return stackTop.getString();
}// end top
// Method to remove the top element of the stack.
// Precondition: The stack exists and is not empty.
// Postcondition: The stack is changed and the top
// element is removed from the stack.
// If the stack is empty, the method throws
// StackUnderflowException
public char pop() throws StackUnderflowException {
if (stackTop == null)
throw new StackUnderflowException();
char data = stackTop.data;
stackTop = stackTop.link; // advance stackTop to the
// next node
return data;
}// end pop
}// end class
=====
public class InToPost {
private LinkedStackClass theStack;
private String input;
private String output = "";
public InToPost(String in) {
input = in;
theStack = new LinkedStackClass();
}
public String doTrans() {
for (int j = 0; j < input.length(); j++) {
char ch = input.charAt(j);
switch (ch) {
case '+':
case '-':
gotOper(ch, 1);
break;
case '*':
case '/':
gotOper(ch, 2);
break;
case '(':
theStack.push(ch);
break;
case ')':
gotParen(ch);
break;
default:
output = output + ch;
break;
}
}
while (!theStack.isEmptyStack()) {
output = output + theStack.pop();
}
return output;
}
public void gotOper(char opThis, int prec1) {
while (!theStack.isEmptyStack()) {
char opTop = theStack.pop();
if (opTop == '(') {
theStack.push(opTop);
break;
}
else {
int prec2;
if (opTop == '+' || opTop == '-')
prec2 = 1;
else
prec2 = 2;
if (prec2 < prec1) {
theStack.push(opTop);
break;
}
else
output = output + opTop;
}
}
theStack.push(opThis);
}
public void gotParen(char ch){
while (!theStack.isEmptyStack()) {
char chx = theStack.pop();
if (chx == '(')
break;
else
output = output + chx;
}
}
}
======
import java.io.*;
import java.util.*;
public class Driver {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("infix.txt");
Scanner scan = new Scanner(file);
String output;
while (scan.hasNextLine()) {
String input = scan.nextLine();
System.out.println("Input: "+input);
InToPost theTrans = new InToPost(input);
output = theTrans.doTrans();
System.out.println("Postfix is " + output + ' ');
}
}
}
===infix.txt===
A+B-C
(A+B)*C
(A+B)/(C-D)
A+((B+C)*(E-F)-G)/(H-I)
A+B*(C+D)-E/F*G+H
===O/P==
Input: A+B-C
Postfix is AB+C-
Input: (A+B)*C
Postfix is AB+C*
Input: (A+B)/(C-D)
Postfix is AB+CD-/
Input: A+((B+C)*(E-F)-G)/(H-I)
Postfix is ABC+EF-*G-HI-/+
Input: A+B*(C+D)-E/F*G+H
Postfix is ABCD+*+EF/G*-H+
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.