// An implementation of queues, based on vectors.
// (c) 1998, 2001 duane a. bailey
// updated for generic types Jim Teresco, October 2005
package structure;
import java.util.Iterator;
//+interface
/**
* An implementation of queues based on vectors.
* 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 which occasionally takes a time proportional to the its
* length to expand.
*
* 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 QueueVector} q = new {@link #QueueVector()};
* 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 QueueList
* @version $Id: QueueVector.java,v 1.2 2005/10/26 17:35:27 terescoj Exp $
* @author, 2001 duane a. bailey
*/
public class QueueVector extends AbstractQueue implements Queue
{
//+constructor
//+private
/**
* The vector that maintains the queue data
*/
protected Vector data;
//-private
/**
* Construct an empty queue
*
* @post constructs an empty queue
*/
public QueueVector()
{
data = new Vector();
}
/**
* Constructs an empty queue with an initial allocation of size.
*
* @post constructs an empty queue of appropriate size
*
* @param size Approximate largest queue size needed.
*/
public QueueVector(int size)
{
data = new Vector(size);
}
//-constructor
//+enqueue
/**
* 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.
*/
public void add(ELTTYPE value)
{
data.add(value);
}
//-enqueue
//+dequeueMethods
/**
* 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()
{
return data.remove(0);
}
//-dequeueMethods
/**
* 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()
{
return data.get(0);
}
//+reuse
/**
* 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()
{
return data.size();
}
/**
* Remove all the values from the queue.
*
* @post removes all elements from the queue
*/
public void clear()
{
data.clear();
}
/**
* 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();
int i;
s.append("");
return s.toString();
}
//+interface
}
//-interface