E
- the type of elements held in this collectionpublic class LinkedQueue<E> extends AQueue<E> implements IQueue<E>
The individual links in a doubly linked list have references to both the preceding and succeeding links in the list. A link in a singly linked list only has a reference to the next link in the list.
A singly linked list is fine for our purposes here. It supports efficient removal of the head, and (if you keep a reference to the tail) it supports efficient adding of new elements to the tail.
created by cspfrederick
and garethhalladay
Fall17
inspired by Chris Wilcox
Modifier and Type | Class and Description |
---|---|
(package private) class |
LinkedQueue.Link
Underlying data structure to create a singly linked list.
|
Constructor and Description |
---|
LinkedQueue(int maxSize)
Creates a queue that will allow up to
maxSize elements. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clears the queue.
|
boolean |
contains(java.lang.Object o)
Returns true if this queue contains the specified element.
|
static void |
main(java.lang.String[] args) |
boolean |
offer(E e)
Adds an element to the queue.
|
E |
peek()
Returns (but does not remove) the head element in the queue.
|
E |
poll()
Removes and returns the oldest element from the queue.
|
int |
size()
Returns the number of elements in this queue.
|
LinkedQueue(int maxSize)
maxSize
elements.maxSize
- The maximum size of the queuemaxCount
public boolean offer(E e)
If the queue was empty, you will need to assign head
and tail
to
a new Link
instance and set count
to one.
Otherwise, create a new Link, and link it in to the end of the queue.
Make sure to set tail
to reference the new Link, and increment count
.
public E poll()
head
to the second Link in the queue, effectively removing the head
head
is now null
, then set head and tail to null,
because that was the last item in the queue, and it is now empty
public E peek()
public int size()
IQueue
public void clear()
public boolean contains(java.lang.Object o)
IQueue
public static void main(java.lang.String[] args)