//******************************************************************** // Demonstrates the use of a user-defined Queue class. //******************************************************************** public class UseQueue { public static void main (String[] args) { Queue word = new Queue(); //adds objects to the queue (of class String here) word.enqueue("One"); word.enqueue("Two"); word.enqueue("Three"); // take elements out of the queue while (!word.isEmpty()) System.out.println (word.dequeue()); } } /* QueueInterface.java */ public interface QueueInterface{ /* isEmpty() returns true if this Queue is empty, false otherwise.*/ public boolean isEmpty(); /* enqueue() inserts an object at the end of the Queue.*/ public void enqueue(Object element); /* dequeue() removes and returns the object at the front of the Queue.*/ public Object dequeue(); } // file Queue.java import java.util.LinkedList; public class Queue implements QueueInterface { private LinkedList queue = new LinkedList(); public void enqueue(Object obj) { // Add an item to back of queue. queue.addLast(obj); } public Object dequeue() { // Remove the next item from the front of the queue. // (Note that queue.removeFirst() both removes an item from the list, and returns the item that // was removed.) return queue.removeFirst(); } public boolean isEmpty() { // Test if the queue is empty. return queue.isEmpty(); } } // end class Queue