Package com.zybooks.dsaj.stackqueue
Class ArrayQueue<E>
java.lang.Object
com.zybooks.dsaj.stackqueue.ArrayQueue<E>
- Type Parameters:
E
- the element type
- All Implemented Interfaces:
Queue<E>
Implementation of the queue ADT using a fixed-length array. All
operations are performed in constant time. An exception is thrown
if an enqueue operation is attempted when the size of the queue is
equal to the length of the array.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionConstructs an empty queue using the default array capacity.ArrayQueue
(int capacity) Constructs and empty queue with the given array capacity. -
Method Summary
Modifier and TypeMethodDescriptiondequeue()
Removes and returns the first element of the queue.void
Adds an element at the rear of the queue.first()
Returns, but does not remove, the first element of the queue.boolean
isEmpty()
Tests whether the queue is empty.int
size()
Returns the number of elements in the queue.toString()
Returns a string representation of the queue as a list of elements.
-
Field Details
-
CAPACITY
public static final int CAPACITYDefault array capacity.- See Also:
-
-
Constructor Details
-
ArrayQueue
public ArrayQueue()Constructs an empty queue using the default array capacity. -
ArrayQueue
public ArrayQueue(int capacity) Constructs and empty queue with the given array capacity.- Parameters:
capacity
- length of the underlying array
-
-
Method Details
-
size
public int size()Returns the number of elements in the queue. -
isEmpty
public boolean isEmpty()Tests whether the queue is empty. -
enqueue
Adds an element at the rear of the queue. This method runs in O(1) time.- Specified by:
enqueue
in interfaceQueue<E>
- Parameters:
e
- new element to be added- Throws:
IllegalStateException
- if the array storing the elements is full
-
first
Returns, but does not remove, the first element of the queue. -
dequeue
Removes and returns the first element of the queue. -
toString
Returns a string representation of the queue as a list of elements. This method runs in O(n) time, where n is the size of the queue.
-