Class LinkedCircularQueue<E>

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

public class LinkedCircularQueue<E> extends Object implements CircularQueue<E>
Realization of a circular FIFO queue as an adaptation of a CircularlyLinkedList. This provides one additional method not part of the general Queue interface. A call to rotate() is a more efficient simulation of the combination enqueue(dequeue()). All operations are performed in constant time.
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an empty queue.
  • Method Summary

    Modifier and Type
    Method
    Description
    Removes and returns the first element of the queue.
    void
    enqueue(E element)
    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.
    void
    Rotates the front element of the queue to the back of the queue.
    int
    Returns the number of elements in the queue.
    Produces a string representation of the contents of the queue.

    Methods inherited from class java.lang.Object

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

    • LinkedCircularQueue

      public LinkedCircularQueue()
      Creates an empty queue.
  • 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 element)
      Adds an element at the rear of the queue.
      Specified by:
      enqueue in interface Queue<E>
      Parameters:
      element - the element to be added
    • 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)
    • rotate

      public void rotate()
      Rotates the front element of the queue to the back of the queue. This is a shorthand for enqueue(dequeue()), except that it does nothing if the queue is empty, and it is implemented more efficiently so that no nodes are created or destroyed.
      Specified by:
      rotate in interface CircularQueue<E>
    • toString

      public String toString()
      Produces a string representation of the contents of the queue. (from front to back). This exists for debugging purposes only.
      Overrides:
      toString in class Object