Package com.zybooks.dsaj.stackqueue
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>
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
-
Method Summary
Modifier and TypeMethodDescriptiondequeue()
Removes and returns the first element of the queue.void
Adds an element at the rear of the queue.first()
Returns, but does not remove, the first element of the queue.boolean
isEmpty()
Tests whether the queue is empty.void
rotate()
Rotates the front element of the queue to the back of the queue.int
size()
Returns the number of elements in the queue.toString()
Produces a string representation of the contents of the queue.
-
Constructor Details
-
LinkedCircularQueue
public LinkedCircularQueue()Creates an empty queue.
-
-
Method Details
-
size
public int size()Returns the number of elements in the queue. -
isEmpty
public boolean isEmpty()Tests whether the queue is empty. -
enqueue
Adds an element at the rear of the queue. -
first
Returns, but does not remove, the first element of the queue. -
dequeue
Removes and returns the first element of the queue. -
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 interfaceCircularQueue<E>
-
toString
Produces a string representation of the contents of the queue. (from front to back). This exists for debugging purposes only.
-