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:
        1. The Queue Invariant is satisfied.
        2. An element x of type E has been given as input.
        Postcondition:
        1. The Queue Invariant is satisfied.
        2. 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:
        1. The Queue Invariant is satisfied.
        Postcondition:
        1. The Queue invariant is satisfied.
        2. The queue is not changed.
        3. If the queue was not empty before this operation then the front element is returned as output.
        4. 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:
        1. The Queue Invariant is satisfied.
        Postcondition:
        1. The Queue Invariant is satisfied.
        2. 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.
        3. 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:
        1. The Queue Invariant is satisfied.
        Postcondition:
        1. The Queue Invariant is satisfied.
        2. The queue is not changed.
        3. If the queue was empty before this operation then “true” is returned as output.
        4. If the queue was not empty before this operation then “false” is returned as output.