Class LinkedQueue<T>
java.lang.Object
LinkedQueue<T>
- Type Parameters:
T
- the type of data contained in the queue
- All Implemented Interfaces:
QueueADT<T>
A generic singly-linked queue implementation.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate 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 TypeMethodDescriptionvoid
clear()
Removes all of the elements from the queue.boolean
Returns true if this queue contains the element, false otherwisedequeue()
Removes and returns the value added to this queue least recentlyvoid
Add a new element to the back of the queue, assumed to be non-null.getList()
Creates a copy of the current contents of this queue in the order they are present here, in ArrayList form.boolean
isEmpty()
Returns true if this queue contains no elements.peek()
Accesses the value added to this queue least recently, without modifying the queueint
size()
Returns the number of elements in the queue.
-
Field Details
-
front
A reference to the LinkedNode currently at the front of the queue, which contains the least-recently added value in the queue. -
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 sizeThe number of values currently present in the queue.
-
-
Method Details
-
enqueue
Description copied from interface:QueueADT
Add a new element to the back of the queue, assumed to be non-null.- Specified by:
enqueue
in interfaceQueueADT<T>
- Parameters:
value
- the value to add
-
dequeue
Description copied from interface:QueueADT
Removes and returns the value added to this queue least recently- Specified by:
dequeue
in interfaceQueueADT<T>
- Returns:
- the least recently-added value, or null if the queue is empty
-
peek
Description copied from interface:QueueADT
Accesses the value added to this queue least recently, without modifying the queue- Specified by:
peek
in interfaceQueueADT<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 interfaceQueueADT<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 interfaceQueueADT<T>
- Returns:
- the number of elements in the queue
-
contains
Description copied from interface:QueueADT
Returns true if this queue contains the element, false otherwise- Specified by:
contains
in interfaceQueueADT<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 interfaceQueueADT<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
-