Package cpsc331.collections
Class ArrayQueue<E>
- java.lang.Object
-
- cpsc331.collections.ArrayQueue<E>
-
- All Implemented Interfaces:
BoundedQueue<E>
public class ArrayQueue<E> extends java.lang.Object implements BoundedQueue<E>
Provides an implementation of a BoundedQueue using a BoundedArray.
The constructor requires time linear in the capacity of the BoundedQueue being created. All other operations require constant time in the worst case.
-
-
Constructor Summary
Constructors Constructor Description ArrayQueue(int maxSize)
Creates an ArrayQueue whose size and capacity are both equal to the input maxSize, and whose entries are all null, if this is a positive int - throwing an IllegalArgumentException if the input maxSize is negative, instead.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
capacity()
Reports the capacity of this bounded queue.void
insert(E x)
Inserts a new entry onto the queue.boolean
isEmpty()
Reports whether the queue is currently empty without changing it.E
peek()
Reports the element at the front of the queue without changing it.E
remove()
Removes an element from the front of the queue and reports it.
-
-
-
Constructor Detail
-
ArrayQueue
public ArrayQueue(int maxSize)
Creates an ArrayQueue whose size and capacity are both equal to the input maxSize, and whose entries are all null, if this is a positive int - throwing an IllegalArgumentException if the input maxSize is negative, instead.- Parameters:
maxSize
- the capacity of the BoundedQueue to be created
-
-
Method Detail
-
insert
public void insert(E x) throws QueueFullException
Description copied from interface:BoundedQueue
Inserts a new entry onto the queue.- Specified by:
insert
in interfaceBoundedQueue<E>
- Parameters:
x
- the object to be inserted onto the queue- Throws:
QueueFullException
- if the queue is already full
Precondition:
- The BoundedQueue Invariant is satisfied.
- An element x of type E has been given as input.
- The BoundedQueue Invariant is satisfied.
- If the number of objects presently on this bounded queue was less than the bounded queue's capacity, then the input object x has been inserted onto the rear of the queue (which is otherwise unchanged).
- If the number of objects presently on the bounded queue was equal to the bounded queue's capacity, then a QueueFullException is thrown and the queue is not changed.
-
peek
public E peek()
Description copied from interface:BoundedQueue
Reports the element at the front of the queue without changing it.- Specified by:
peek
in interfaceBoundedQueue<E>
- Returns:
- the element at the front of the queue
-
remove
public E remove()
Description copied from interface:BoundedQueue
Removes an element from the front of the queue and reports it.- Specified by:
remove
in interfaceBoundedQueue<E>
- Returns:
- the element removed from the queue
-
isEmpty
public boolean isEmpty()
Description copied from interface:BoundedQueue
Reports whether the queue is currently empty without changing it.- Specified by:
isEmpty
in interfaceBoundedQueue<E>
- Returns:
- true if the queue is empty, and false otherwise
Precondition:
- The BoundedQueue Invariant is satisfied.
- The BoundedQueue Invariant is satisfied.
- The bounded queue is not changed.
- If the bounded queue was empty before this operation then “true” is returned as output.
- If the bounded queue was not empty before this operation then “false” is returned as output.
-
capacity
public int capacity()
Description copied from interface:BoundedQueue
Reports the capacity of this bounded queue.- Specified by:
capacity
in interfaceBoundedQueue<E>
- Returns:
- the capacity of this BoundedQueue
Precondition:
- The BoundedQueue Invariant is satisfied.
- The BoundedQueue Invariant is satisfied.
- The bounded queue is not changed.
- The capacity of this bounded queue is returned as output.
-
-