structure
Class QueueArray<ELTTYPE>

java.lang.Object
  extended by structure.AbstractStructure<ELTTYPE>
      extended by structure.AbstractLinear<ELTTYPE>
          extended by structure.AbstractQueue<ELTTYPE>
              extended by structure.QueueArray<ELTTYPE>
All Implemented Interfaces:
java.lang.Iterable<ELTTYPE>, Linear<ELTTYPE>, Queue<ELTTYPE>, Structure<ELTTYPE>

public class QueueArray<ELTTYPE>
extends AbstractQueue<ELTTYPE>
implements Queue<ELTTYPE>

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.AbstractLinear.empty()){
          char c = ((Character)q.AbstractQueue.dequeue()).charValue();
          unicodeSum+=Character.getNumericValue(c);
     }

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

See Also:
QueueList, QueueVector

Constructor Summary
QueueArray(int size)
          Construct a queue holding at most size elements.
 
Method Summary
 void add(ELTTYPE value)
          Add a value to the tail of the queue.
 void clear()
          Remove all the values from the queue.
 ELTTYPE 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.util.Iterator<ELTTYPE> iterator()
          Returns an iterator for traversing the structure.
 ELTTYPE 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 structure.AbstractQueue
dequeue, enqueue, getFirst, peek
 
Methods inherited from class structure.AbstractLinear
empty, remove
 
Methods inherited from class structure.AbstractStructure
contains, elements, hashCode, values
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Queue
dequeue, empty, enqueue, getFirst, peek
 
Methods inherited from interface structure.Structure
contains, elements, remove, values
 

Constructor Detail

QueueArray

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

Parameters:
size - The maximum size of the queue.
Post:
create a queue capable of holding at most size values
Method Detail

add

public void add(ELTTYPE value)
Add a value to the tail of the queue.

Specified by:
add in interface Linear<ELTTYPE>
Specified by:
add in interface Queue<ELTTYPE>
Specified by:
add in interface Structure<ELTTYPE>
Parameters:
value - The value added.
See Also:
AbstractQueue.enqueue(ELTTYPE)
Pre:
the queue is not full
Post:
the value is added to the tail of the structure

remove

public ELTTYPE remove()
Remove a value from the head of the queue.

Specified by:
remove in interface Linear<ELTTYPE>
Specified by:
remove in interface Queue<ELTTYPE>
Returns:
The value actually removed.
See Also:
AbstractQueue.dequeue()
Pre:
the queue is not empty
Post:
the head of the queue is removed and returned

get

public ELTTYPE get()
Fetch the value at the head of the queue.

Specified by:
get in interface Linear<ELTTYPE>
Specified by:
get in interface Queue<ELTTYPE>
Returns:
Reference to the first value of the queue.
Pre:
the queue is not empty
Post:
the element at the head of the queue is returned

size

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

Specified by:
size in interface Linear<ELTTYPE>
Specified by:
size in interface Queue<ELTTYPE>
Specified by:
size in interface Structure<ELTTYPE>
Returns:
The number of elements within the queue.
Post:
returns the number of elements in the queue

clear

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

Specified by:
clear in interface Structure<ELTTYPE>
Post:
removes all elements 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.
Post:
returns true if the queue is at its capacity

isEmpty

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

Specified by:
isEmpty in interface Structure<ELTTYPE>
Overrides:
isEmpty in class AbstractStructure<ELTTYPE>
Returns:
True iff the queue is empty.
Post:
returns true iff the queue is empty

iterator

public java.util.Iterator<ELTTYPE> iterator()
Description copied from interface: Structure
Returns an iterator for traversing the structure.

Specified by:
iterator in interface java.lang.Iterable<ELTTYPE>
Specified by:
iterator in interface Structure<ELTTYPE>
Returns:
an iterator for traversing the structure
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()

toString

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

Overrides:
toString in class java.lang.Object
Returns:
String representing the queue.
Post:
returns string representation of queue