Class Queue<Item>

Object
  extended by Queue<Item>
All Implemented Interfaces:
Iterable<Item>

public class Queue<Item>
extends Object
implements Iterable<Item>

The Queue class represents a first-in-first-out (FIFO) queue of generic items. It supports the usual enqueue and dequeue operations, along with methods for peeking at the top item, testing if the queue is empty, and iterating through the items in FIFO order.

All queue operations except iteration are constant time.

For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.


Constructor Summary
Queue()
          Create an empty queue.
 
Method Summary
 Item dequeue()
          Remove and return the item on the queue least recently added.
 void enqueue(Item item)
          Add the item to the queue.
 boolean isEmpty()
          Is the queue empty?
 java.util.Iterator<Item> iterator()
          Return an iterator that iterates over the items on the queue in FIFO order.
static void main(String[] args)
          A test client.
 Item peek()
          Return the item least recently added to the queue.
 int size()
          Return the number of items in the queue.
 String toString()
          Return string representation.
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Queue

public Queue()
Create an empty queue.

Method Detail

isEmpty

public boolean isEmpty()
Is the queue empty?


size

public int size()
Return the number of items in the queue.


peek

public Item peek()
Return the item least recently added to the queue. Throw an exception if the queue is empty.


enqueue

public void enqueue(Item item)
Add the item to the queue.


dequeue

public Item dequeue()
Remove and return the item on the queue least recently added. Throw an exception if the queue is empty.


toString

public String toString()
Return string representation.

Overrides:
toString in class Object

iterator

public java.util.Iterator<Item> iterator()
Return an iterator that iterates over the items on the queue in FIFO order.

Specified by:
iterator in interface Iterable<Item>

main

public static void main(String[] args)
A test client.