/* Submitted by: Maderal, Kurt Merkell John P. // ICS 20 A // Xavier University // // // // An implementation of a List-Based Queues */ import java.io.*; public class QueueList { public BufferedReader ItemReader; private int size, check; private Node head, tail; private String programName; private String inp3; public QueueList(String n) throws IOException { programName = n; size=0; check=0; System.out.println("\n -------------------- List-Based Queue --------------------\n"); instruct(); access(); } public void enqueue(Object obj) { System.out.println(" Enqueued : "+ obj); Node node = new Node(); node.setElement(obj); node.setNext(null); // node will be new tail node if (size==0) { head = node; // special case if a previously empty queue } else tail.setNext(node); //add node at the tail of the list tail = node; //update the reference to the tail node size++; } public void dequeue() { Object obj=null; if (size==0) System.out.println(" Error..!!! "+ programName.toUpperCase() +" is Empty..."); else obj = head.getElement(); head = head.getNext(); size--; if (size == 0) tail = null; // the queue is now empty System.out.println(" Dequeued : "+ obj.toString()); } //inner class Node for the reference public class Node { //instance variables private Object element; private Node next; //simple constructor public Node() { this(null,null); } public Node (Object o, Node n) { element=o; next=n; } //accessor methods public Object getElement() { return element; } public Node getNext() { return next; } //modifier methods public void setElement(Object newElem) { element=newElem; } public void setNext(Node newNext) { next=newNext; } } //main public static void main(String args[]) throws IOException { QueueList list = new QueueList("List-Based Queue"); } // method instructions public void instruct() { System.out.println("\nCommands for Methods: type \" e \" for "+"\"Enqueue\""+" with your Input..."); System.out.println(" \" d \" for "+"\"Dequeue\""); System.out.println(" \" f \" for "+"\"Front\""); System.out.println(" \" y \" for "+"\"IsEmpty?\""); System.out.println(" \" r \" for "+"\"Reset All\""); System.out.println(" \" a \" for "+"\"Displaying all Inputs\""); System.out.println(" \" i \" for "+"\"Commands / Instructions / HELP\""); System.out.println(" \" x \" for "+"\"Exit Program\"\n\n"); System.out.println(" --------> NOTE : NO SPACES.... <--------\n\n"); } //method access() for starting public void access() throws IOException { // "infinite for loop" so that inputs are ask over and over // if check == -1 then loop terminates and reset method is called for (; ; ) { inputs(); if (check==-1) { break; } } reset(); } //reset method that calls for a new Object of type Queues public void reset() throws IOException { QueueList ql = new QueueList("List-Based Queues"); } //method inputs() public void inputs() throws IOException { ItemReader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Type Command: "); inp3 = ItemReader.readLine(); String com3 = (inp3.charAt(0)+""); if ( (com3.equals("e"))||(com3.equals("d"))||(com3.equals("f"))||(com3.equals("y"))||(com3.equals("x"))||(com3.equals("r"))||(com3.equals("a"))||(com3.equals("i")) ) { if (com3.equals("e")) { String s3 = ""; for (int i=1; i inputs ( ) //display all the remaining inputs from the list public void displayInputs() { Node current = head; if (size==0) { System.out.println(" Error..!!! "+ programName.toUpperCase() +" is Empty..."); return; } System.out.print("\nALL Stored inputs :-->> "); while (current != null) { System.out.print(current.element.toString()+ ", ");//.next from class Node current = current.next; } System.out.println("\n"); } // method IsEmpty : returns " true " if queue is Empty, " false " otherwise public boolean IsEmpty( ) { return size==0; } // method front : return and show the item / element that will be dequeued next public void front() { if (size==0) { System.out.println(" Error..!!! "+ programName.toUpperCase() +" is Empty..."); } else System.out.println(" Front : "+ head.getElement().toString() ); } }