- Type Parameters:
E
- the list element type
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Deque<E>
,List<E>
,Queue<E>
DoublyLinkedList
implements a doubly linked List
data structure, that exposes its
ListNodes
where the data is stored in.
An element holding ListNode
can be removed or added to a DoublyLinkedList
in
constant time O(1). Other methods that operate on ListNodes
directly also have constant
runtime. This is also the case for methods that operate on the first(head) and last(tail) node or
element. Random access methods have a runtime O(n) that is linearly dependent on the size of the
DoublyLinkedList
.
A DoublyLinkedList
supports null
elements but does not support
null ListNodes
. This class is not thread safe and needs to be synchronized externally if
modified by concurrent threads.
The iterators over this list have a fail-fast behavior meaning that they throw a
ConcurrentModificationException
after they detect a structural modification of the list,
that they're not responsible for.
This class is similar to LinkedList
. The general difference is that the ListNodes
of this List
are accessible and can be removed or added directly. To ensure the integrity
of the List
nodes of this List have a reference to the List they belong to. This
increases the memory occupied by this list implementation compared to LinkedList
for the
same elements. Instances of LinkedList.Node
have three references each (the element, next
and previous), instances of DoublyLinkedList.ListNode
have four (the element, next,
previous and the list).
- Author:
- Timofey Chudakov, Hannes Wellmann
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Container for the elements stored in aDoublyLinkedList
.static interface
static interface
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addElementBeforeNode
(DoublyLinkedList.ListNode<E> successor, E element) Inserts the specified element before the specifiedsuccessor
in this list.addElementFirst
(E element) Inserts the specified element at the front of this list.addElementLast
(E element) Inserts the specified element at the end of this list.void
void
void
addNode
(int index, DoublyLinkedList.ListNode<E> node) Inserts the specifiednode
at the specified position in this list.void
addNodeBefore
(DoublyLinkedList.ListNode<E> node, DoublyLinkedList.ListNode<E> successor) Inserts the specifiednode
before the specifiedsuccessor
in this list.void
Inserts the specifiednode
at the front of this list.void
Inserts the specifiednode
at the end of this list.void
append
(DoublyLinkedList<E> movedList) Appends themovedList
to the end of this list.circularIterator
(E firstElement) Returns aDoublyLinkedList.NodeIterator
that starts at the firstDoublyLinkedList.ListNode
of this list that is equal to the specifiedfirstElement
, iterates in forward direction over the end of this list until the first node.void
clear()
boolean
Returns true if thisDoublyLinkedList
contains the specifiedDoublyLinkedList.ListNode
.element()
get
(int index) getFirst()
Returns the firstnode
of this list.getLast()
Returns the lastnode
of this list.getNode
(int index) Returns thenode
at the specified position in this list.int
Returns the index of the specifiednode
in this list, or -1 if this list does not contain thenode
.void
invert()
Inverts the list.boolean
isEmpty()
iterator()
lastNodeOf
(Object element) Returns the lastnode
holding the specifiedelement
in this list.listIterator
(int index) listIterator
(E element) Returns aDoublyLinkedList.ListNodeIterator
over the elements in this list (in proper sequence) starting with the firstDoublyLinkedList.ListNode
whose value is equal to the specifiedelement
.void
moveFrom
(int index, DoublyLinkedList<E> movedList) Moves allListNodes
of the givensourceList
to this list and inserts them all before the node previously at the given position.Returns the firstnode
holding the specifiedelement
in this list.boolean
boolean
offerFirst
(E e) boolean
peek()
peekLast()
poll()
pollLast()
pop()
void
prepend
(DoublyLinkedList<E> movedList) Prepends themovedList
to the beginning of this list.void
remove()
remove
(int index) boolean
boolean
boolean
removeNode
(DoublyLinkedList.ListNode<E> node) Removes thenode
from this list.reverseCircularIterator
(E firstElement) Returns aDoublyLinkedList.NodeIterator
that starts at the firstDoublyLinkedList.ListNode
of this list that is equal to the specifiedfirstElement
, iterates in reverse direction over the end of this list until the first node.int
size()
Methods inherited from class java.util.AbstractSequentialList
addAll, set
Methods inherited from class java.util.AbstractList
add, equals, hashCode, indexOf, lastIndexOf, removeRange, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
addAll, contains, containsAll, remove, removeAll, replaceAll, retainAll, sort, spliterator, toArray, toArray
-
Constructor Details
-
DoublyLinkedList
public DoublyLinkedList()
-
-
Method Details
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceList<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
size
public int size() -
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceList<E>
- Overrides:
clear
in classAbstractList<E>
-
addNode
Inserts the specifiednode
at the specified position in this list.This method has a linear runtime complexity O(n) that depends linearly on the distance of the index to the nearest end. Adding
node
as first or last takes only constant time O(1).- Parameters:
index
- index at which the specifiednode
is to be insertednode
- the node to add- Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index > size()
)IllegalArgumentException
- ifnode
is already part of this or anotherDoublyLinkedList
NullPointerException
- ifnode
isnull
-
addNodeFirst
Inserts the specifiednode
at the front of this list.This method has constant runtime complexity O(1).
- Parameters:
node
- the node to add- Throws:
IllegalArgumentException
- ifnode
is already part of this or anotherDoublyLinkedList
NullPointerException
- ifnode
isnull
-
addNodeLast
Inserts the specifiednode
at the end of this list.This method has constant runtime complexity O(1).
- Parameters:
node
- the node to add- Throws:
IllegalArgumentException
- ifnode
is already part of this or anotherDoublyLinkedList
NullPointerException
- ifnode
isnull
-
addNodeBefore
public void addNodeBefore(DoublyLinkedList.ListNode<E> node, DoublyLinkedList.ListNode<E> successor) Inserts the specifiednode
before the specifiedsuccessor
in this list.This method has constant runtime complexity O(1).
- Parameters:
node
- the node to addsuccessor
-ListNode
before which thenode
is inserted- Throws:
IllegalArgumentException
- ifnode
is already contained in this or anotherDoublyLinkedList
orsuccessor
is not contained in this listNullPointerException
- ifsuccessor
ornode
isnull
-
getFirstNode
Returns the firstnode
of this list.This method has constant runtime complexity O(1).
- Returns:
- the first
ListNode
of this list - Throws:
NoSuchElementException
- if this list is empty
-
getLastNode
Returns the lastnode
of this list.This method has constant runtime complexity O(1).
- Returns:
- the last
ListNode
of this list - Throws:
NoSuchElementException
- if this list is empty
-
getNode
Returns thenode
at the specified position in this list.This method has linear runtime complexity O(n).
- Parameters:
index
- index of theListNode
to return- Returns:
- the
ListNode
at the specified position in this list - Throws:
IndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= size()
)
-
indexOfNode
Returns the index of the specifiednode
in this list, or -1 if this list does not contain thenode
.More formally, returns the index
i
such thatnode == getNode(i)
, or -1 if there is no such index. Because aListNode
is contained in at most one list exactly once, the returned index (if not -1) is the only occurrence of thatnode
.This method has linear runtime complexity O(n) to find
node
but returns in constant time O(1) ifnode
is notcontained
in this list.- Parameters:
node
- the node to search for- Returns:
- the index of the specified
node
in this list, or -1 if this list does not containnode
- Throws:
NullPointerException
- ifnode
isnull
-
containsNode
Returns true if thisDoublyLinkedList
contains the specifiedDoublyLinkedList.ListNode
.This method has constant runtime complexity O(1).
- Parameters:
node
- the node whose presence in thisDoublyLinkedList
is to be tested- Returns:
- true if this
DoublyLinkedList
contains theDoublyLinkedList.ListNode
- Throws:
NullPointerException
- ifnode
isnull
-
removeNode
Removes thenode
from this list. Returns true ifnode
was in this list and is now removed. Ifnode
is not contained in this list, the list is left unchanged.This method has constant runtime complexity O(1).
- Parameters:
node
- the node to remove from this list- Returns:
- true if node was removed from this list
- Throws:
NullPointerException
- ifnode
isnull
-
nodeOf
Returns the firstnode
holding the specifiedelement
in this list. More formally, returns the firstListNode
such thatObjects.equals(element, node.getValue())
, ornull
if there is no such node.This method has linear runtime complexity O(n).
- Parameters:
element
- the element whoseListNode
is to return- Returns:
- the first
ListNode
holding theelement
or null if no node was found
-
lastNodeOf
Returns the lastnode
holding the specifiedelement
in this list. More formally, returns the lastListNode
such thatObjects.equals(element, node.getValue())
, ornull
if there is no such node.This method has linear runtime complexity O(n).
- Parameters:
element
- the element whoseListNode
is to return- Returns:
- the last
ListNode
holding theelement
or null if no node was found
-
addElementFirst
Inserts the specified element at the front of this list. Returns theDoublyLinkedList.ListNode
allocated to store thevalue
. The returnedListNode
is the new head of the list.This method is equivalent to
addFirst(Object)
but returns the allocatedListNode
.- Parameters:
element
- the element to add- Returns:
- the
ListNode
allocated to store thevalue
-
addElementLast
Inserts the specified element at the end of this list. Returns theDoublyLinkedList.ListNode
allocated to store thevalue
. The returnedListNode
is the new tail of the list.This method is equivalent to
addLast(Object)
but returns the allocatedListNode
.- Parameters:
element
- the element to add- Returns:
- the
ListNode
allocated to store thevalue
-
addElementBeforeNode
public DoublyLinkedList.ListNode<E> addElementBeforeNode(DoublyLinkedList.ListNode<E> successor, E element) Inserts the specified element before the specifiedsuccessor
in this list. Returns theListNode
allocated to store thevalue
.- Parameters:
successor
-ListNode
before which the node holdingvalue
is insertedelement
- the element to add- Returns:
- the
ListNode
allocated to store thevalue
- Throws:
IllegalArgumentException
- ifsuccessor
is not contained in this listNullPointerException
- ifsuccessor
isnull
-
add
-
get
-
remove
-
addFirst
-
addLast
-
offerFirst
- Specified by:
offerFirst
in interfaceDeque<E>
-
offerLast
-
removeFirst
- Specified by:
removeFirst
in interfaceDeque<E>
-
removeLast
- Specified by:
removeLast
in interfaceDeque<E>
-
pollFirst
-
pollLast
-
getFirst
-
getLast
-
peekFirst
-
peekLast
-
removeFirstOccurrence
- Specified by:
removeFirstOccurrence
in interfaceDeque<E>
-
removeLastOccurrence
- Specified by:
removeLastOccurrence
in interfaceDeque<E>
-
offer
-
remove
-
poll
-
element
-
peek
-
push
-
pop
-
invert
public void invert()Inverts the list. For instance, calling this method on the list $(a,b,c,\dots,x,y,z)$ will result in the list $(z,y,x,\dots,c,b,a)$. This method does only pointer manipulation, meaning that all the list nodes allocated for the previously added elements are valid after this method finishes. -
moveFrom
Moves allListNodes
of the givensourceList
to this list and inserts them all before the node previously at the given position. All thenodes
ofmovedList
are moved to this list. When this method terminates this list contains all nodes ofmovedList
andmovedList
is empty.- Parameters:
index
- index of the first element oflist
in thislist
after it was addedmovedList
- theDoublyLinkedList
to move to this one- Throws:
NullPointerException
- ifmovedList
isnull
-
append
Appends themovedList
to the end of this list. All the elements frommovedList
are transferred to this list, i.e. thelist
is empty after calling this method.- Parameters:
movedList
- theDoublyLinkedList
to append to this one- Throws:
NullPointerException
- ifmovedList
isnull
-
prepend
Prepends themovedList
to the beginning of this list. All the elements frommovedList
are transferred to this list, i.e. themovedList
is empty after calling this method.- Parameters:
movedList
- theDoublyLinkedList
to prepend to this one- Throws:
NullPointerException
- ifmovedList
isnull
-
circularIterator
Returns aDoublyLinkedList.NodeIterator
that starts at the firstDoublyLinkedList.ListNode
of this list that is equal to the specifiedfirstElement
, iterates in forward direction over the end of this list until the first node.The first call to
DoublyLinkedList.NodeIterator.nextNode()
returns the firstnode
that holds a value such thatObjects.equals(node.getValue, firstElement)
returnstrue
. The returnedNodeIterator
iterates in forward direction returning the respective next element in subsequent calls tonext(Node)
. The returned iterator ignores the actual bounds of thisDoublyLinkedList
and iterates until the node before the first one is reached. ItshasNext()
returnsfalse
if the next node would be the first one.- Parameters:
firstElement
- the element equal to the firstnext()
- Returns:
- a circular
NodeIterator
iterating forward fromfirstElement
-
reverseCircularIterator
Returns aDoublyLinkedList.NodeIterator
that starts at the firstDoublyLinkedList.ListNode
of this list that is equal to the specifiedfirstElement
, iterates in reverse direction over the end of this list until the first node.The first call to
DoublyLinkedList.NodeIterator.nextNode()
returns the firstnode
that holds a value such thatObjects.equals(node.getValue, firstElement)
returnstrue
. The returnedNodeIterator
iterates in reverse direction returning the respective previous element in subsequent calls tonext(Node)
. The returned iterator ignores the actual bounds of thisDoublyLinkedList
and iterates until the node before the first one is reached. ItshasNext()
returnsfalse
if the next node would be the first one.- Parameters:
firstElement
- the element equal to the firstnext()
- Returns:
- a circular
NodeIterator
iterating backwards fromfirstElement
-
descendingIterator
- Specified by:
descendingIterator
in interfaceDeque<E>
-
iterator
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
-
listIterator
- Specified by:
listIterator
in interfaceList<E>
- Specified by:
listIterator
in classAbstractSequentialList<E>
-
listIterator
Returns aDoublyLinkedList.ListNodeIterator
over the elements in this list (in proper sequence) starting with the firstDoublyLinkedList.ListNode
whose value is equal to the specifiedelement
.- Parameters:
element
- the first element to be returned from the list iterator (by a call to thenext
method)- Returns:
- a list iterator over the elements in this list (in proper sequence)
- Throws:
NoSuchElementException
- ifelement
is not in the list
-