Class DoublyLinkedList<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- java.util.AbstractSequentialList<E>
-
- org.jgrapht.util.DoublyLinkedList<E>
-
- Type Parameters:
E
- the list element type
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Deque<E>
,List<E>
,Queue<E>
public class DoublyLinkedList<E> extends AbstractSequentialList<E> implements Deque<E>
DoublyLinkedList
implements a doubly linkedList
data structure, that exposes itsListNodes
where the data is stored in.An element holding
ListNode
can be removed or added to aDoublyLinkedList
in constant time O(1). Other methods that operate onListNodes
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 theDoublyLinkedList
.A
DoublyLinkedList
supportsnull
elements but does not supportnull 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 theListNodes
of thisList
are accessible and can be removed or added directly. To ensure the integrity of theList
nodes of this List have a reference to the List they belong to. This increases the memory occupied by this list implementation compared toLinkedList
for the same elements. Instances ofLinkedList.Node
have three references each (the element, next and previous), instances ofDoublyLinkedList.ListNode
have four (the element, next, previous and the list).- Author:
- Timofey Chudakov, Hannes Wellmann
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
DoublyLinkedList.ListNode<V>
Container for the elements stored in aDoublyLinkedList
.static interface
DoublyLinkedList.ListNodeIterator<E>
static interface
DoublyLinkedList.NodeIterator<E>
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description DoublyLinkedList()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(int index, E element)
DoublyLinkedList.ListNode<E>
addElementBeforeNode(DoublyLinkedList.ListNode<E> successor, E element)
Inserts the specified element before the specifiedsuccessor
in this list.DoublyLinkedList.ListNode<E>
addElementFirst(E element)
Inserts the specified element at the front of this list.DoublyLinkedList.ListNode<E>
addElementLast(E element)
Inserts the specified element at the end of this list.void
addFirst(E e)
void
addLast(E e)
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
addNodeFirst(DoublyLinkedList.ListNode<E> node)
Inserts the specifiednode
at the front of this list.void
addNodeLast(DoublyLinkedList.ListNode<E> node)
Inserts the specifiednode
at the end of this list.void
append(DoublyLinkedList<E> movedList)
Appends themovedList
to the end of this list.DoublyLinkedList.NodeIterator<E>
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
containsNode(DoublyLinkedList.ListNode<E> node)
Returns true if thisDoublyLinkedList
contains the specifiedDoublyLinkedList.ListNode
.DoublyLinkedList.NodeIterator<E>
descendingIterator()
E
element()
E
get(int index)
E
getFirst()
DoublyLinkedList.ListNode<E>
getFirstNode()
Returns the firstnode
of this list.E
getLast()
DoublyLinkedList.ListNode<E>
getLastNode()
Returns the lastnode
of this list.DoublyLinkedList.ListNode<E>
getNode(int index)
Returns thenode
at the specified position in this list.int
indexOfNode(DoublyLinkedList.ListNode<E> node)
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()
DoublyLinkedList.NodeIterator<E>
iterator()
DoublyLinkedList.ListNode<E>
lastNodeOf(Object element)
Returns the lastnode
holding the specifiedelement
in this list.DoublyLinkedList.ListNodeIterator<E>
listIterator()
DoublyLinkedList.ListNodeIterator<E>
listIterator(int index)
DoublyLinkedList.ListNodeIterator<E>
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.DoublyLinkedList.ListNode<E>
nodeOf(Object element)
Returns the firstnode
holding the specifiedelement
in this list.boolean
offer(E e)
boolean
offerFirst(E e)
boolean
offerLast(E e)
E
peek()
E
peekFirst()
E
peekLast()
E
poll()
E
pollFirst()
E
pollLast()
E
pop()
void
prepend(DoublyLinkedList<E> movedList)
Prepends themovedList
to the beginning of this list.void
push(E e)
E
remove()
E
remove(int index)
E
removeFirst()
boolean
removeFirstOccurrence(Object o)
E
removeLast()
boolean
removeLastOccurrence(Object o)
boolean
removeNode(DoublyLinkedList.ListNode<E> node)
Removes thenode
from this list.DoublyLinkedList.NodeIterator<E>
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
-
-
-
-
Method Detail
-
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
public void addNode(int index, DoublyLinkedList.ListNode<E> node)
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
public void addNodeFirst(DoublyLinkedList.ListNode<E> node)
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
public void addNodeLast(DoublyLinkedList.ListNode<E> node)
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
public DoublyLinkedList.ListNode<E> 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
public DoublyLinkedList.ListNode<E> 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
public DoublyLinkedList.ListNode<E> getNode(int index)
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
public int indexOfNode(DoublyLinkedList.ListNode<E> node)
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
public boolean containsNode(DoublyLinkedList.ListNode<E> node)
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
public boolean removeNode(DoublyLinkedList.ListNode<E> node)
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
public DoublyLinkedList.ListNode<E> nodeOf(Object element)
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
public DoublyLinkedList.ListNode<E> lastNodeOf(Object element)
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
public DoublyLinkedList.ListNode<E> addElementFirst(E element)
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
public DoublyLinkedList.ListNode<E> addElementLast(E element)
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
public void add(int index, E element)
-
get
public E get(int index)
-
remove
public E remove(int index)
-
offerFirst
public boolean offerFirst(E e)
- Specified by:
offerFirst
in interfaceDeque<E>
-
removeFirst
public E removeFirst()
- Specified by:
removeFirst
in interfaceDeque<E>
-
removeLast
public E removeLast()
- Specified by:
removeLast
in interfaceDeque<E>
-
removeFirstOccurrence
public boolean removeFirstOccurrence(Object o)
- Specified by:
removeFirstOccurrence
in interfaceDeque<E>
-
removeLastOccurrence
public boolean removeLastOccurrence(Object o)
- Specified by:
removeLastOccurrence
in interfaceDeque<E>
-
offer
public boolean offer(E e)
-
remove
public E remove()
-
poll
public E poll()
-
element
public E element()
-
peek
public E peek()
-
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
public 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. 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
public void append(DoublyLinkedList<E> movedList)
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
public void prepend(DoublyLinkedList<E> movedList)
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
public DoublyLinkedList.NodeIterator<E> 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.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
public DoublyLinkedList.NodeIterator<E> 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.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
public DoublyLinkedList.NodeIterator<E> descendingIterator()
- Specified by:
descendingIterator
in interfaceDeque<E>
-
iterator
public DoublyLinkedList.NodeIterator<E> iterator()
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> listIterator()
- Specified by:
listIterator
in interfaceList<E>
- Overrides:
listIterator
in classAbstractList<E>
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> listIterator(int index)
- Specified by:
listIterator
in interfaceList<E>
- Specified by:
listIterator
in classAbstractSequentialList<E>
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> 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
.- 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
-
-