© 1998-2002 McGraw-Hill

structure
Interface Queue

All Superinterfaces:
Linear, Structure
All Known Implementing Classes:
AbstractQueue, QueueList, QueueArray, QueueVector

public interface Queue
extends Linear

Interface describing a first-in, first-out structure. Values are added at the tail, and removed from the head. Queues are typically used to process values in the order that they appear and to store the state of a buffered object. The structure package provides several implementations of the Queue interface, each of which has its particular strengths and weaknesses.

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)
 {
     Queue q = new 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.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);
 }
 

See Also:
Stack, AbstractQueue, QueueArray, QueueVector, QueueList

Method Summary
 void add(Object value)
          Add a value to the tail of the queue.
 Object dequeue()
          Remove a value from the head of the queue.
 boolean empty()
          Returns true iff the queue is empty.
 void enqueue(Object value)
          Add a value to the tail of the queue.
 Object get()
          Fetch the value at the head of the queue.
 Object getFirst()
          Fetch the value at the head of the queue.
 Object peek()
          Fetch the value at the head of the queue.
 Object remove()
          Remove a value form the head of the queue.
 int size()
          Returns the number of elements in the queue.
 
Methods inherited from interface structure.Structure
clear, contains, elements, isEmpty, iterator, remove, values
 

Method Detail

add

public void add(Object value)
Add a value to the tail of the queue.
Specified by:
add in interface Linear
Parameters:
value - The value added.
Postcondition:
the value is added to the tail of the structure
See Also:
enqueue(java.lang.Object)

enqueue

public void enqueue(Object value)
Add a value to the tail of the queue.
Parameters:
value - The value to be added.
Postcondition:
the value is added to the tail of the structure

remove

public Object remove()
Remove a value form the head of the queue.
Specified by:
remove in interface Linear
Precondition:
the queue is not empty
Postcondition:
the head of the queue is removed and returned
Returns:
The value actually removed.
See Also:
dequeue()

dequeue

public Object dequeue()
Remove a value from the head of the queue.
Precondition:
the queue is not empty
Postcondition:
the head of the queue is removed and returned
Returns:
The value removed from the queue.

getFirst

public Object getFirst()
Fetch the value at the head of the queue.
Precondition:
the queue is not empty
Postcondition:
the element at the head of the queue is returned
Returns:
Reference to the first value of the queue.

get

public Object get()
Fetch the value at the head of the queue.
Specified by:
get in interface Linear
Precondition:
the queue is not empty
Postcondition:
the element at the head of the queue is returned
Returns:
Reference to the first value of the queue.

peek

public Object peek()
Fetch the value at the head of the queue.
Precondition:
the queue is not empty
Postcondition:
the element at the head of the queue is returned
Returns:
Reference to the first value of the queue.

empty

public boolean empty()
Returns true iff the queue is empty. Provided for compatibility with java.util.Vector.empty.
Specified by:
empty in interface Linear
Postcondition:
returns true if and only if the queue is empty
Returns:
True iff the queue is empty.

size

public int size()
Returns the number of elements in the queue.
Specified by:
size in interface Linear
Postcondition:
returns the number of elements in the queue
Returns:
number of elements in queue.

© 1998-2002 McGraw-Hill