Class ArrayQueue<Item>

java.lang.Object
dsa.lab03.solutions.ArrayQueue<Item>
Type Parameters:
Item - the item type
All Implemented Interfaces:
Container<Item>, Queue<Item>, Iterable<Item>

public class ArrayQueue<Item> extends Object implements Queue<Item>
An array queue.

Implements the queue interface by using a circular dynamic array.

  • Constructor Details

    • ArrayQueue

      public ArrayQueue()
      Construct an empty array queue.
    • ArrayQueue

      public ArrayQueue(Iterable<Item> items)
      Construct an array queue containing the given items.
      Parameters:
      items - the items
    • ArrayQueue

      public ArrayQueue(Iterable<Item> items, int size) throws IllegalArgumentException
      Construct an array queue containing the given items more efficiently than ArrayQueue(Iterable).
      Parameters:
      items - the items
      size - the number of items
      Throws:
      IllegalArgumentException - if size != n (where n is items's size)
    • ArrayQueue

      @SafeVarargs public ArrayQueue(Item... items)
      Construct an array queue containing the given items.
      Parameters:
      items - the items
  • Method Details

    • enqueue

      public void enqueue(Item item)
      Description copied from interface: Queue
      Enqueue (insert) the given item to the back.
      Specified by:
      enqueue in interface Queue<Item>
      Parameters:
      item - the new back item
    • dequeue

      public Item dequeue() throws NoSuchElementException
      Description copied from interface: Queue
      Dequeue (remove and return) the given item from the front.
      Specified by:
      dequeue in interface Queue<Item>
      Returns:
      the old front item
      Throws:
      NoSuchElementException - if there's no first item to remove (i.e. if this is empty)
    • front

      public Item front() throws NoSuchElementException
      Description copied from interface: Queue
      Get the item at the front.
      Specified by:
      front in interface Queue<Item>
      Returns:
      the front item
      Throws:
      NoSuchElementException - if there's no first item to retrieve (i.e. if this is empty)
    • size

      public int size()
      Description copied from interface: Container
      Get the number of contained items.
      Specified by:
      size in interface Container<Item>
      Returns:
      the size
    • items

      public Iterable<Item> items()
      Description copied from interface: Container
      Get an iterable that yields each item once.
      Specified by:
      items in interface Container<Item>
      Returns:
      an iterable over the items