package asd_library.queue;

import java.util.*;

/**
 * Implementation of the abstract data type Queue
 */

public class Queue implements Queue_adt, Cloneable {

  private java.util.LinkedList list= new java.util.LinkedList();

  public Queue() {
  }

  /**
   * Stores an item at the queue end.
   * @param el object to store.
   */

  public void enqueue(Object el){
    list.add(el);
  }

  /**
  * Retrieves the item from the queue head and removes it.
  * @return the object deleted from the queue.
  */
  public Object dequeue(){
    return  list.removeFirst();
  }


  /**
   * Retrieves the item from the queue head but doesn't remove it.
   * @return the object at the head of the queue.
   */
  public Object firstEl(){
    return list.getFirst();
  }


  /**
   * Verifies if the queue is empty.
   * @return true if the queue is empty, false otherwise.
   */
  public boolean isEmpty(){
    return list.isEmpty();
  }

  /**
   * Returns a string representation of the queue.
   * The string representation consists of a list of the collection's elements
   * in the order they are returned by its iterator, enclosed in square brackets
   * ("[]"). Adjacent elements are separated by the characters ", " (comma and
   * space). Elements are converted to strings as by String.valueOf(Object).
   * @return a <b>String</b> that represents the queue.
   */
  public String toString(){
    return list.toString();
    }

    /**
     * Returns a shallow copy of this Queue. (The elements themselves are not cloned.)
     * @return a shallow copy of this Queue instance.
     */
    public Object clone(){
    try{
      Queue q = (Queue)super.clone();
      q.list = (java.util.LinkedList) list.clone();
      return q;
    }catch(CloneNotSupportedException e){
      //It mustn't happen
      System.out.println("Cloning not allowed");
      return this;
    }
  }


  /**
   * Compares the specified object with this queue for equality.
   * Returns true if and only if the specified object is also a queue, both
   * queues have the same size, and all corresponding pairs of elements in the
   * two queues are equal. (Two elements e1 and e2 are equal
   * if (e1==null ? e2==null : e1.equals(e2)).) In other words, two queues are
   * defined to be equal if they contain the same elements in the same order.
   * @param q the object to be compared for equality with this queue
   * @return true if the specified object is equal to this queue.

   */
  public boolean equals(Object q)
  {
    return list.equals(((Queue)q).list);
  }


  /**
   * Returns a list-iterator of the elements in this queue (in proper sequence)
   * @return a ListIterator of the elements in this queue.
   */
  public ListIterator listIterator(){

    return this.list.listIterator();

  }



}



 


Bacheca di Algoritmi e Strutture Dati a.a. 2007-08 - canale A - L

forum del corso

ultima modifica: 03/04/2008 23.35
by FdA