Package cpsc331.collections
Interface Queue<E>
-
- All Known Implementing Classes:
ListQueue
public interface Queue<E>
Provides an interface for an unbounded queue whose elements are of type E.
Queue Invariant: A collection of objects of type E is maintained in first-in first-out order: The object that is visible at the front of the queue is the object that has was first inserted onto it (that has not yet been removed).
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
insert(E x)
Inserts a new object onto the queue.boolean
isEmpty()
Reports whether the queue is currently empty without changing it.E
peek()
Reports the front element of the queue without changing it.E
remove()
Removes an element from the front of the stack and reports it.
-
-
-
Method Detail
-
insert
void insert(E x)
Inserts a new object onto the queue.- Parameters:
x
- the object to be pushed onto the queue
Precondition:
- The Queue Invariant is satisfied.
- An element x of type E has been given as input.
- The Queue Invariant is satisfied.
- The input object x has been inserted at the rear of the queue (which is otherwise unchanged).
-
peek
E peek()
Reports the front element of the queue without changing it.- Returns:
- the element at the front of the queue
- Throws:
java.util.NoSuchElementException
- if the queue is empty
Precondition:
- The Queue Invariant is satisfied.
- The Queue invariant is satisfied.
- The queue is not changed.
- If the queue was not empty before this operation then the front element is returned as output.
- If the queue was empty before this operation then a NoSuchElementException is thrown.
-
remove
E remove()
Removes an element from the front of the stack and reports it.- Returns:
- the element removed from the queue
- Throws:
java.util.NoSuchElementException
- if the queue is already empty
Precondition:
- The Queue Invariant is satisfied.
- The Queue Invariant is satisfied.
- If the queue was not empty before this operation then the front element is removed from the queue (which is otherwise unchanged) and this element is returned as output.
- If the queue was empty before this operation then a NoSuchElementException is thrown and the queue is not changed.
-
isEmpty
boolean isEmpty()
Reports whether the queue is currently empty without changing it.- Returns:
- true if the queue is empty, and false otherwise
Precondition:
- The Queue Invariant is satisfied.
- The Queue Invariant is satisfied.
- The queue is not changed.
- If the queue was empty before this operation then “true” is returned as output.
- If the queue was not empty before this operation then “false” is returned as output.
-
-