public class ResizingArrayQueue<Item> extends java.lang.Object implements java.lang.Iterable<Item>
This implementation uses a resizing array, which doubles when it is full and halves when it is one-quarter full. The enqueue and dequeue operations take constant amortized time. The size, peek, and isEmpty operations takes constant time in the worst case.
For additional documentation, see Section 1.3 of "Algorithms, 4th Edition", by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
ResizingArrayQueue()
Initializes an empty queue.
|
Modifier and Type | Method and Description |
---|---|
Item |
dequeue()
Removes and returns the item on this queue that was least recently added.
|
void |
enqueue(Item item)
Adds the item to this queue.
|
boolean |
isEmpty()
Is this queue empty?
|
java.util.Iterator<Item> |
iterator()
Returns an iterator that iterates over the items in this queue in FIFO order.
|
static void |
main(java.lang.String[] args)
Unit tests the ResizingArrayQueue data type.
|
Item |
peek()
Returns the item least recently added to this queue,
but does not remove the item from the queue.
|
int |
size()
Returns the number of items in this queue.
|
public boolean isEmpty()
public int size()
public void enqueue(Item item)
public Item dequeue()
java.util.NoSuchElementException
- if this queue is emptypublic Item peek()
java.util.NoSuchElementException
- if this queue is emptypublic java.util.Iterator<Item> iterator()
iterator
in interface java.lang.Iterable<Item>
public static void main(java.lang.String[] args)