Package algs13

Class Queue<T>

java.lang.Object
algs13.Queue<T>
All Implemented Interfaces:
Iterable<T>

public class Queue<T> extends Object implements Iterable<T>
The Queue class represents a first-in-first-out (FIFO) queue of generic items. It supports the usual enqueue and dequeue operations, along with methods for peeking at the top item, testing if the queue is empty, and iterating through the items in FIFO order.

All queue operations except iteration are constant time.

For additional documentation, see Section 1.3 of Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.

  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Create an empty queue.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    Remove and return the item on the queue least recently added.
    void
    enqueue(T item)
    Add the item to the queue.
    boolean
    Is the queue empty?
    Return an iterator that iterates over the items on the queue in FIFO order.
    static void
    main(String[] args)
    A test client.
    Return the item least recently added to the queue.
    int
    Return the number of items in the queue.
    void
    toGraphviz(String filename)
     
    Return string representation.

    Methods inherited from class java.lang.Object Link icon

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable Link icon

    forEach, spliterator
  • Constructor Details Link icon

    • Queue Link icon

      public Queue()
      Create an empty queue.
  • Method Details Link icon

    • isEmpty Link icon

      public boolean isEmpty()
      Is the queue empty?
    • size Link icon

      public int size()
      Return the number of items in the queue.
    • peek Link icon

      public T peek()
      Return the item least recently added to the queue.
      Throws:
      NoSuchElementException - if queue is empty.
    • enqueue Link icon

      public void enqueue(T item)
      Add the item to the queue.
    • dequeue Link icon

      public T dequeue()
      Remove and return the item on the queue least recently added.
      Throws:
      NoSuchElementException - if queue is empty.
    • toString Link icon

      public String toString()
      Return string representation.
      Overrides:
      toString in class Object
    • iterator Link icon

      public Iterator<T> iterator()
      Return an iterator that iterates over the items on the queue in FIFO order.
      Specified by:
      iterator in interface Iterable<T>
    • toGraphviz Link icon

      public void toGraphviz(String filename)
    • main Link icon

      public static void main(String[] args)
      A test client.