Question: Java - Using the LinkedQueueClass And LinkedStackClass to make this program public class LinkedQueueClass { //Definition of the node private QueueNode queueFront; //reference variable to
Java - Using the LinkedQueueClass And LinkedStackClass to make this program

public class LinkedQueueClass { //Definition of the node private QueueNode queueFront; //reference variable to the //first element of the queue private QueueNode queueRear; //reference variable to the //last element of the queue //default constructor public LinkedQueueClass() { queueFront = null; queueRear = null; }//end constructor //Method to initialize the queue to an empty state. //Postcondition: queueFront = null; queueRear = null public void initializeQueue() { queueFront = null; queueRear = null; }//end initializeQueue //Method to determine whether the queue is empty. //Postcondition: Returns true if the queue is empty; // otherwise, returns false. public boolean isEmptyQueue() { return (queueFront == null); }//end isEmptyQueue //Method to return the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the method throws // QueueUnderflowException; otherwise, // the value of the first element // of the queue is returned. public String front() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); else { return queueFront.getString(); } }//end front //Method to return the last element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: If the queue is empty, the method throws // QueueUnderflowException; otherwise, // the value of the last element // of the queue is returned. public String back() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); return queueRear.getString(); } //Method to add queueElement to the queue. //Precondition: The queue exists. //Postcondition: The queue is changed and newNode // is added to the queue. public void addQueue(String newElement) { QueueNode newNode = new QueueNode(newElement); //create the node newNode.link = null; //initialize the link field to null if(queueFront == null) //if initially the queue is empty { queueFront = newNode; queueRear = newNode; } else //add newNode at the end { queueRear.link = newNode; queueRear = queueRear.link; } }//end addQueue //Method to remove the first element of the queue. //Precondition: The queue exists and is not empty. //Postcondition: The queue is changed and the first // element is removed from the queue. public void deleteQueue() throws QueueUnderflowException { if(isEmptyQueue()) throw new QueueUnderflowException(); queueFront = queueFront.link; //advance queueFront if(queueFront == null) //if after deletion the queue is queueRear = null; //empty, set queueRear to null } //end deleteQueue }//end Class public class QueueNode { String info; //Data type of Queue Node QueueNode link; //Reference to next node in queue //default constructor public QueueNode() { info = ""; } //constructor with a parameter public QueueNode(String data) { info = data; } //Method to set the value of the instance variable num. //Postcondition: info = data; public void setString(String data) { info = data; } //Method to return the value of the instance variable num. //Postcondition: The value of num is returned. public String getString() { return info; } public boolean equals(String otherElement) { return (info.equals(otherElement)); } public int compareTo(String otherElement) { return (info.compareTo(otherElement)); } public void setNext (QueueNode nextNode) //link to next node in Queue { //assign pointer to next node link = nextNode; }//end nextNode public QueueNode getNext() //get next link or go to the next node in Queue { //get the pointer return link; }//end getNext public String toString() { return String.valueOf(info); } } public class QueueException extends RuntimeException { public QueueException() { } public QueueException(String msg) { super(msg); } } public class QueueUnderflowException extends QueueException { public QueueUnderflowException() { super("Queue Underflow"); } public QueueUnderflowException(String msg) { super(msg); } } public class QueueOverflowException extends QueueException { public QueueOverflowException() { super("Queue Overflow"); } public QueueOverflowException(String msg) { super(msg); } }
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 /ewNode 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 /ext 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); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
