Class LinkedQueue<T>

java.lang.Object
LinkedQueue<T>
Type Parameters:
T - the type of data contained in the queue
All Implemented Interfaces:
QueueADT<T>

public class LinkedQueue<T> extends Object implements QueueADT<T>
A generic singly-linked queue implementation.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private LinkedNode<T>
    A reference to the LinkedNode currently at the back of the queue, which contains the most-recently added value in the queue.
    private LinkedNode<T>
    A reference to the LinkedNode currently at the front of the queue, which contains the least-recently added value in the queue.
    private int
    The number of values currently present in the queue.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Removes all of the elements from the queue.
    boolean
    contains(T value)
    Returns true if this queue contains the element, false otherwise
    Removes and returns the value added to this queue least recently
    void
    enqueue(T value)
    Add a new element to the back of the queue, assumed to be non-null.
    Creates a copy of the current contents of this queue in the order they are present here, in ArrayList form.
    boolean
    Returns true if this queue contains no elements.
    Accesses the value added to this queue least recently, without modifying the queue
    int
    Returns the number of elements in the queue.

    Methods inherited from class java.lang.Object

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

    • front

      private LinkedNode<T> front
      A reference to the LinkedNode currently at the front of the queue, which contains the least-recently added value in the queue.
    • back

      private LinkedNode<T> back
      A reference to the LinkedNode currently at the back of the queue, which contains the most-recently added value in the queue.
    • size

      private int size
      The number of values currently present in the queue.
  • Method Details

    • enqueue

      public void enqueue(T value)
      Description copied from interface: QueueADT
      Add a new element to the back of the queue, assumed to be non-null.
      Specified by:
      enqueue in interface QueueADT<T>
      Parameters:
      value - the value to add
    • dequeue

      public T dequeue()
      Description copied from interface: QueueADT
      Removes and returns the value added to this queue least recently
      Specified by:
      dequeue in interface QueueADT<T>
      Returns:
      the least recently-added value, or null if the queue is empty
    • peek

      public T peek()
      Description copied from interface: QueueADT
      Accesses the value added to this queue least recently, without modifying the queue
      Specified by:
      peek in interface QueueADT<T>
      Returns:
      the least recently-added value, or null if the queue is empty
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: QueueADT
      Returns true if this queue contains no elements.
      Specified by:
      isEmpty in interface QueueADT<T>
      Returns:
      true if the queue contains no elements, false otherwise
    • size

      public int size()
      Description copied from interface: QueueADT
      Returns the number of elements in the queue.
      Specified by:
      size in interface QueueADT<T>
      Returns:
      the number of elements in the queue
    • contains

      public boolean contains(T value)
      Description copied from interface: QueueADT
      Returns true if this queue contains the element, false otherwise
      Specified by:
      contains in interface QueueADT<T>
      Parameters:
      value - the value to check
      Returns:
      true if the queue contains the element, false otherwise
    • clear

      public void clear()
      Description copied from interface: QueueADT
      Removes all of the elements from the queue. The queue will be empty after this call returns.
      Specified by:
      clear in interface QueueADT<T>
    • getList

      public ArrayList<T> getList()
      Creates a copy of the current contents of this queue in the order they are present here, in ArrayList form. This method should traverse the queue without removing any elements, and add the values (not the nodes!) to an ArrayList in the order they appear in the queue, with the front of the queue at index 0.
      Returns:
      an ArrayList representation of the current state of this queue