Guest User

Untitled

a guest
Apr 26th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.82 KB | None | 0 0
  1. package javafoundations;
  2. import javafoundations.exceptions.*;
  3.  
  4. public class LinkedQueue<T> implements QueueADT<T>
  5. {
  6.    private int count;
  7.    private LinearNode<T> front, rear;
  8.  
  9.    /**
  10.     * Creates an empty queue.
  11.     */
  12.    public LinkedQueue()
  13.    {
  14.       count = 0;
  15.       front = rear = null;
  16.    }
  17.  
  18.    /**
  19.     * Adds the specified element to the rear of this queue.
  20.     *
  21.     * @param element  the element to be added to the rear of this queue
  22.     */
  23.    public void enqueue (T element)
  24.    {
  25.       LinearNode<T> node = new LinearNode<T>(element);
  26.  
  27.       if (isEmpty())
  28.          front = node;
  29.       else
  30.          rear.setNext (node);
  31.  
  32.       rear = node;
  33.       count++;
  34.    }
  35.  
  36.    /**
  37.     * Removes the element at the front of this queue and returns a
  38.     * reference to it. Throws an EmptyCollectionException if the
  39.     * queue is empty.
  40.     *
  41.     * @return                           the element at the front of this queue
  42.     * @throws EmptyCollectionException  if an empty collection exception occurs
  43.     */
  44.    public T dequeue() throws EmptyCollectionException
  45.    {       
  46.        if(isEmpty())
  47.        {           
  48.            throw new EmptyCollectionException("queue");
  49.        }
  50.        else
  51.        {
  52.            front = front.getNext();
  53.        }
  54.        
  55.        count--;
  56.        return front.getElement();
  57.    }
  58.    
  59.    /**
  60.     * Returns a reference to the element at the front of this queue.
  61.     * The element is not removed from the queue.  Throws an
  62.     * EmptyCollectionException if the queue is empty.  
  63.     *
  64.     * @return                            a reference to the first element in
  65.     *                                    this queue
  66.     * @throws EmptyCollectionsException  if an empty collection exception occurs
  67.     */
  68.    public T first() throws EmptyCollectionException
  69.    {
  70.        if(front == null)
  71.        {
  72.            throw new EmptyCollectionException("queue");
  73.        }
  74.        else
  75.        {
  76.            return front.getElement();
  77.        }
  78.    }
  79.  
  80.    /**
  81.     * Returns true if this queue is empty and false otherwise.
  82.     *
  83.     * @return  true if this queue is empty and false if otherwise
  84.     */
  85.    public boolean isEmpty()
  86.    {
  87.         if(front == null)
  88.         {
  89.             return true;
  90.         }
  91.         else
  92.         {
  93.             return false;
  94.         }
  95.    }
  96.  
  97.    /**
  98.     * Returns the number of elements currently in this queue.
  99.     *
  100.     * @return  the integer representation of the size of this queue
  101.     */
  102.    public int size()
  103.    {
  104.        System.out.println(count);
  105.        return (count);
  106.    }
  107.  
  108.    /**
  109.     * Returns a string representation of this queue.
  110.     *
  111.     * @return  the string representation of this queue
  112.     */
  113.    public String toString()
  114.    {   
  115.        while(front.getNext() != null)
  116.        {
  117.            System.out.println(front);
  118.            front = front.getNext();
  119.            count--;
  120.        }
  121.         return ("There are " + count + " elements in this queue.");
  122.    }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment