//class to represent one node in a list class ListNode<T> { //package access mem
ID: 3710135 • Letter: #
Question
//class to represent one node in a list class ListNode<T> { //package access members; List can access these directly T data; ListNode<T> nextNode; //constructor creates a ListNode that refers to object ListNode( T object ) { this( object, null ); } // end ListNode one-argument constructor //constructor creates ListNode that refers to //Object and to next ListNode ListNode( T object, ListNode<T> node ) { data = object; nextNode = node; } // end ListNode two-argument constructor //return reference to data in node T getObject() { return data; // return Object in this node } // end method getObject //return reference to next node in list ListNode<T> getNext() { return nextNode; // get next node } // end method getNext } // end class ListNode //class List definition public class List<T> { private static final String Integer = null; private ListNode<T> firstNode; private ListNode<T> lastNode; private String name; // string like "list" used in printing //constructor creates empty List with "list" as the name public List() { this( "list" ); } // end List no-argument constructor //constructor creates an empty List with a name public List( String listName ) { name = listName; firstNode = lastNode = null; } // end List one-argument constructor //insert Object at front of List public void insertAtFront( T insertItem ) { if ( isEmpty() ) // firstNode and lastNode refer to same object firstNode = lastNode = new ListNode<T>( insertItem ); else // firstNode refers to new node firstNode = new ListNode<T>( insertItem, firstNode ); } // end method insertAtFront //insert Object at end of List public void insertAtBack( T insertItem ) { if ( isEmpty() ) // firstNode and lastNode refer to same Object firstNode = lastNode = new ListNode<T>( insertItem ); else // lastNode's nextNode refers to new node lastNode = lastNode.nextNode = new ListNode<T>( insertItem ); } // end method insertAtBack //remove first node from List public T removeFromFront() throws RuntimeException { if ( isEmpty() ) // throw exception if List is empty throw new RuntimeException( name+" is Empty !!" ); T removedItem = firstNode.data; // retrieve data being removed //update references firstNode and lastNode if ( firstNode == lastNode ) firstNode = lastNode = null; else firstNode = firstNode.nextNode; return removedItem; // return removed node data } // end method removeFromFront //remove last node from List public T removeFromBack() throws RuntimeException { if ( isEmpty() ) // throw exception if List is empty throw new RuntimeException( name+" is Empty !!" ); T removedItem = lastNode.data; // retrieve data being removed // update references firstNode and lastNode if ( firstNode == lastNode ) firstNode = lastNode = null; else // locate new last node { ListNode<T> current = firstNode; //loop while current node does not refer to lastNode while ( current.nextNode != lastNode ) current = current.nextNode; lastNode = current; // current is new lastNode current.nextNode = null; } // end else return removedItem; // return removed node data } // end method removeFromBack //determine whether list is empty public boolean isEmpty() { return firstNode == null; // return true if List is empty } // end method isEmpty //output List contents public void print() { if ( isEmpty() ) { System.out.printf( "Empty %s ", name ); return; } // end if System.out.printf( "The %s is: ", name ); ListNode<T> current = firstNode; //while not at end of list, output current node's data while ( current != null ) { System.out.printf( "%s ", current.data ); current = current.nextNode; } // end while System.out.println( " " ); } // end method print } // end class List
//=============
//class to represent Stack public class Stack<T> { List<T> stackList; // constructor Stack() { this("My Stack"); } // constructor Stack(String name) { stackList=new List<T>(name); } public void push( T item ) { stackList.insertAtFront( item ); } public T pop() throws RuntimeException { return stackList.removeFromFront(); } public boolean isEmpty() { return stackList.isEmpty(); } public void print() { stackList.print(); } } // end class Stack
//===============
//class to represent Queue public class Queue<T> { List<T> queueList; // constructor Queue() { this("My Queue"); } // constructor Queue(String name) { queueList=new List<T>(name); } public void enqueue( T item ) { queueList.insertAtBack( item ); } public T dequeue() throws RuntimeException { return queueList.removeFromFront(); } public boolean isEmpty() { return queueList.isEmpty(); } public void print() { queueList.print(); } } // end class Queue
========
(in java program language)
Us sing only the given QueueExplanation / Answer
Please find below the code in Runner.java with three methods corresponding to three questions:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Runner {
static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//Solution a.
public static void methodA() throws IOException{
System.out.println("Enter 4 strings: ");
String first = reader.readLine();
String second = reader.readLine();
String third = reader.readLine();
String fourth = reader.readLine();
//Creating a stack
Stack<String> stack = new Stack<String>();
List<String> list = new List<String>();
list.insertAtFront(first);
list.insertAtFront(second);
list.insertAtFront(third);
list.insertAtFront(fourth);
System.out.print("Stack: ");
stack.print();
//Now to append items in reverse order
stack.push(list.removeFromBack());
stack.push(list.removeFromBack());
stack.push(list.removeFromBack());
stack.push(list.removeFromBack());
stack.print();
}
//Solution b.
public static void methodB() throws IOException{
System.out.println("Enter 5 int: ");
int first = Integer.valueOf(reader.readLine());
int second = Integer.valueOf(reader.readLine());
int third = Integer.valueOf(reader.readLine());
int fourth = Integer.valueOf(reader.readLine());
int fifth = Integer.valueOf(reader.readLine());
//Creating queues
Queue<Integer> firstQueue = new Queue<Integer>();
Stack<Integer> buffer = new Stack<Integer>();
firstQueue.enqueue(first);
firstQueue.enqueue(second);
firstQueue.enqueue(third);
firstQueue.enqueue(fourth);
firstQueue.enqueue(fifth);
//printing before reverse
firstQueue.print();
while(!firstQueue.isEmpty())
buffer.push(firstQueue.dequeue());
while(!buffer.isEmpty())
firstQueue.enqueue(buffer.pop());
//printing after reverse
firstQueue.print();
}
//Solution c.
public static void methodC() throws IOException{
System.out.println("Enter 5 int: ");
int first = Integer.valueOf(reader.readLine());
int second = Integer.valueOf(reader.readLine());
int third = Integer.valueOf(reader.readLine());
int fourth = Integer.valueOf(reader.readLine());
int fifth = Integer.valueOf(reader.readLine());
//Creating queues
Queue<Integer> queue = new Queue<Integer>();
Queue<Integer> bufferQueue2 = new Queue<Integer>();
queue.enqueue(first);
queue.enqueue(second);
queue.enqueue(third);
queue.enqueue(fourth);
queue.enqueue(fifth);
//printing before
queue.print();
int minimum = queue.dequeue();
while(!queue.isEmpty()){
int value = queue.dequeue();
if(value < minimum){
minimum = value;
}
bufferQueue2.enqueue(value);
}
queue.enqueue(minimum);
while(!bufferQueue2.isEmpty())
queue.enqueue(bufferQueue2.dequeue());
//printing after
queue.print();
}
public static void main(String [] args) throws IOException{
methodA();
methodB();
methodC();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.