### Implementing a Queue using a list

• Recall: list operations

• We have learned about these list operations:

 insert a new value at the start of a list insert a new value at the tail of a list remove a new value at the start of a list remove a new value at the tail of a list

• The Queue Interface

• The Queue interface:

 ``` public interface Queue { public void enqueue(double x) throws Exception; // Full queue; public double dequeue() throws Exception; // Empty queue; } ```

• Implementing a Queue using a list

• How to implement a queue using a list:

 Enqueue: inserts a new value at the tail of the list Dequeue: deletes the value at the head of the list

Graphically:

• Code in Java:

Note: there is nothing new to the code !!!

I just cut and paste the put() and remove methods from previous programs

 ``` public class ListQueue implements Queue { /* ========================================== Node "inner class" ========================================== */ public class Node { double value; Node next; public Node( double x ) { value = x; next = null; } public String toString() { return "" + value; } } public Node head; // head points to first Node of the list // **** Access SHOULD be set to private **** // I use public to print stuff easier.... // Constructor public ListQueue() { head = null; // Make an empty list } /* ==================================================== enqueue(x ): store value x into the list - at tail This is the same method as "put()" method in the "insert at tail" list program ==================================================== */ public void enqueue( double x ) { Node e; e = new Node( x ); // Make node with value x if ( head == null ) { /* ------------------------------ Insert e into an empty list ------------------------------ */ head = e; // e is the first elem. e.next = null; // Mark e as the last Node. } else { /* ------------------------------ Insert e into a non-empty list ------------------------------ */ // Find the last Node in the list Node ptr; ptr = head; while ( ptr.next != null ) ptr = ptr.next; // ptr NOW points to the last Node in list // Add e AFTER last Node ptr.next = e; // Make the last node point to e e.next = null; // Mark e as the last Node } } /* ==================================================== dequeue(): delete the head node in list Return the value that was stored in the elemenet This is the same method as "remove()" method in the "insert at head" list program ==================================================== */ public double dequeue( ) throws Exception { if ( head == null ) throw new Exception("Remove from empty list"); double r; r = head.value; // Save return value head = head.next; // Delete elem at head return r; // Return the value saved } } ```

• Test program:

 ``` public static void main( String[] args ) throws Exception { Queue myQ = new ListQueue(); double x; myQ.enqueue(1.0); System.out.println("enqueue(1.0): " + "myQ = " + myQ); myQ.enqueue(2.0); System.out.println("enqueue(2.0): " + "myQ = " + myQ); myQ.enqueue(3.0); System.out.println("enqueue(3.0): " + "myQ = " + myQ); System.out.println("\nBefore dequeue:"); System.out.println("myQ = " + myQ); x = myQ.dequeue(); System.out.println("x = " + x + ", myQ = " + myQ); x = myQ.dequeue(); System.out.println("x = " + x + ", myQ = " + myQ); x = myQ.dequeue(); System.out.println("x = " + x + ", myQ = " + myQ); } ```

Output:

 ``` enqueue(1.0): myQ = [1.0] enqueue(2.0): myQ = [1.0 , 2.0] enqueue(3.0): myQ = [1.0 , 2.0 , 3.0] Before dequeue: myQ = [1.0 , 2.0 , 3.0] x = 1.0, myQ = [2.0 , 3.0] x = 2.0, myQ = [3.0] x = 3.0, myQ = [] ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg