/**
 * Queue linked list class makes use of composition in 
 * that it "has-a" reference to a LinkedList class object 
 * that it creates in the constructor.
 * The Queue class then "delegates" the Queue method calls 
 * to the appropriate LinkedList methods.            
 * @author Suzanne Balik, 16 Oct 2001 
 * based on code in Java How to Program by Deitel & Deitel
 */
public class Queue {

  private LinkedList list;
  
  public Queue() {
    list = new LinkedList();
  }
  
  public boolean isEmpty() {
    return list.isEmpty();
  }
  
  public void enqueue(int info) {
    
    list.addToRear(info);
    
  }
  
  public int dequeue() throws EmptyListException {
  
    if (isEmpty())
      
      throw new EmptyListException("Queue is empty!");
      
    else
    
      return list.removeFromFront();
  }
  
  
  
  public String toString() {
    return list.toString();
  } 
  
  public static void main(String[] args) {

  Queue queue = new Queue();
  
  //Catch EmptyListException's
  try {  
    
    
    System.out.println("Queue empty? " + queue.isEmpty());
  
    queue.enqueue(5);
  
    queue.enqueue(9);
    
    queue.enqueue(4);
    
    queue.enqueue(8);
  
    System.out.println("Queue empty? " + queue.isEmpty());
  
    System.out.println(queue);
  
    while (!queue.isEmpty()) {
  
      System.out.println("dequeued from queue: " + 
                          queue.dequeue());
  
      System.out.println(queue);
  
    }
    
    //See what happens if we dequeue empty queue
    queue.dequeue();
  }
  
  catch (EmptyListException e) {
    System.err.println(e);
  }  
    
    //See what happens if we dequeue empty queue without 
    //catching exception
    queue.dequeue();
  }
}