planet.util
Class Queue

java.lang.Object
  extended byplanet.util.Queue
All Implemented Interfaces:
java.io.Serializable

public class Queue
extends java.lang.Object
implements java.io.Serializable

An implementation of queues based on arrays. The head of the queue starts out at the head of the array, allowing the queue to grow and shrink in constant time. This queue implementation is ideal for applications that require a queue with a known maximum size that expands 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)
 {
     int charsInInput = QueueExample.countChars(argument);
     QueueArray q = new QueueArray(charsInInput);
     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.enqueue(new Character(arguments[i].charAt(j)));
	       }
	   }
     }

     while(!q.#empty()){
	  char c = ((Character)q.#dequeue()).charValue();
	  unicodeSum+=Character.getNumericValue(c);
     }

     System.out.println("Total Value: " + unicodeSum);
 }
 

Version:
$Id: Queue.java,v 1.1 2004/03/11 11:22:31 jordi Exp $
See Also:
QueueList, QueueVector, Serialized Form

Field Summary
protected  int count
          current size of queue
protected  java.lang.Object[] data
          The references to values stored within the queue.
protected  int head
          index of the head of queue.
 
Constructor Summary
Queue(int size)
          Construct a queue holding at most size elements.
 
Method Summary
 void add(java.lang.Object value)
          Add a value to the tail of the queue.
 void clear()
          Remove all the values from the queue.
 java.lang.Object get()
          Fetch the value at the head of the queue.
 boolean isEmpty()
          Determine if the queue is empty.
 boolean isFull()
          Determines if the queue is not able to accept any new values.
 java.lang.Object remove()
          Remove a value from the head of the queue.
 int size()
          Determine the number of elements within the queue
 java.lang.String toString()
          Construct a string representation of the queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

data

protected java.lang.Object[] data
The references to values stored within the queue.


head

protected int head
index of the head of queue.


count

protected int count
current size of queue

Constructor Detail

Queue

public Queue(int size)
Construct a queue holding at most size elements.

Parameters:
size - The maximum size of the queue.
Method Detail

add

public void add(java.lang.Object value)
         throws QueueFull
Add a value to the tail of the queue.

Parameters:
value - The value added.
Throws:
QueueFull
See Also:
#enqueue

remove

public java.lang.Object remove()
Remove a value from the head of the queue.

Returns:
The value actually removed.
See Also:
#dequeue

get

public java.lang.Object get()
Fetch the value at the head of the queue.

Returns:
Reference to the first value of the queue.

size

public int size()
Determine the number of elements within the queue

Returns:
The number of elements within the queue.

clear

public void clear()
Remove all the values from the queue.


isFull

public boolean isFull()
Determines if the queue is not able to accept any new values.

Returns:
True iff the queue is full.

isEmpty

public boolean isEmpty()
Determine if the queue is empty.

Returns:
True iff the queue is empty.

toString

public java.lang.String toString()
Construct a string representation of the queue.

Returns:
String representing the queue.