// Implementation of queues using linked lists.
// (c) 1998, 2001 duane a. bailey
package structure;
import java.util.Iterator;
//+interface
/**
* An implementation of queues based on circular lists.
* The head of the queue is stored at the head of the list, allowing the queue to
* grow and shrink in constant time.
* This queue implementation is ideal for applications that require a dynamically
* resizable queue that resizes in constant time.
*
* Example usage:
*
* To compute the sum of the unicode value of every character in the standard input
* we could use the following:
*
*
* public static void main(String[] arguments)
* {
* {@link QueueList} q = new {@link #QueueList()};
* int unicodeSum = 0;
*
* if(arguments.length > 0){
* for(int i=0; i < arguments.length; i++){
* for(int j=0; j < arguments[i].length(); j++){
* q.{@link #enqueue(Object) enqueue(new Character(arguments[i].charAt(j)))};
* }
* }
* }
*
* while(!q.{@link #empty()}){
* char c = ((Character)q.{@link #dequeue()}).charValue();
* unicodeSum+=Character.getNumericValue(c);
* }
*
* System.out.println("Total Value: " + unicodeSum);
* }
*
* @see QueueArray
* @see QueueVector
* @version $Id: QueueList.java,v 1.2 2005/10/18 19:47:22 terescoj Exp $
* @author, 2001 duane a. bailey
*/
public class QueueList extends AbstractQueue implements Queue
{
//-interface
//+private
/**
* The list holding queue values in order.
*/
protected List data;
//-private
//+constructor
//+interface
/**
* Construct a new queue with no data.
*
* @post Constructs a new, empty queue
*/
public QueueList()
//-interface
{
data = new CircularList();
}
//-constructor
//+interface
//+mutators
/**
* Add a value to the tail of the queue.
*
* @post The value is added to the tail of the structure
*
* @param value The value added.
* @see #enqueue
*/
public void add(ELTTYPE value)
//-interface
{
data.addLast(value);
}
//+interface
/**
* Remove a value from the head of the queue.
*
* @pre The queue is not empty
* @post The head of the queue is removed and returned
*
* @return The value actually removed.
* @see #dequeue
*/
public ELTTYPE remove()
//-interface
{
return data.removeFirst();
}
//-mutators
//+interface
//+reuse
/**
* Fetch the value at the head of the queue.
*
* @pre The queue is not empty
* @post The element at the head of the queue is returned
*
* @return Reference to the first value of the queue.
*/
public ELTTYPE get()
//-interface
{
return data.getFirst();
}
//+interface
/**
* Determine the number of elements within the queue.
*
* @post Returns the number of elements in the queue
*
* @return The number of elements within the queue.
*/
public int size()
//-interface
{
return data.size();
}
//+interface
/**
* Remove all the values from the queue.
*
* @post Removes all elements from the queue
*/
public void clear()
//-interface
{
data.clear();
}
//+interface
/**
* Determine if the queue is empty.
*
* @post Returns true iff the queue is empty
*
* @return True iff the queue is empty.
*/
public boolean isEmpty()
//-interface
{
return data.isEmpty();
}
//-reuse
public Iterator iterator()
{
return data.iterator();
}
/**
* Construct a string representation of the queue.
*
* @post Returns string representation of queue
*
* @return String representing the queue.
*/
public String toString()
{
StringBuffer s = new StringBuffer();
s.append("");
return s.toString();
}
//+interface
}
//-interface