Class ArrayQueue<E>

java.lang.Object
com.zybooks.dsaj.stackqueue.ArrayQueue<E>
Type Parameters:
E - the element type
All Implemented Interfaces:
Queue<E>

public class ArrayQueue<E> extends Object implements 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

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default array capacity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an empty queue using the default array capacity.
    ArrayQueue(int capacity)
    Constructs and empty queue with the given array capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    Removes and returns the first element of the queue.
    void
    Adds an element at the rear of the queue.
    Returns, but does not remove, the first element of the queue.
    boolean
    Tests whether the queue is empty.
    int
    Returns the number of elements in the queue.
    Returns a string representation of the queue as a list of elements.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • 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.
      Specified by:
      size in interface Queue<E>
      Returns:
      number of elements in the queue
    • isEmpty

      public boolean isEmpty()
      Tests whether the queue is empty.
      Specified by:
      isEmpty in interface Queue<E>
      Returns:
      true if the queue is empty, false otherwise
    • enqueue

      public void enqueue(E e) throws IllegalStateException
      Adds an element at the rear of the queue. This method runs in O(1) time.
      Specified by:
      enqueue in interface Queue<E>
      Parameters:
      e - new element to be added
      Throws:
      IllegalStateException - if the array storing the elements is full
    • first

      public E first()
      Returns, but does not remove, the first element of the queue.
      Specified by:
      first in interface Queue<E>
      Returns:
      the first element of the queue (or null if empty)
    • dequeue

      public E dequeue()
      Removes and returns the first element of the queue.
      Specified by:
      dequeue in interface Queue<E>
      Returns:
      element removed (or null if empty)
    • toString

      public String 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.
      Overrides:
      toString in class Object
      Returns:
      textual representation of the queue.