- 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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceContainer for the elements stored in aDoublyLinkedList.static interfacestatic interface -
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidaddElementBeforeNode(DoublyLinkedList.ListNode<E> successor, E element) Inserts the specified element before the specifiedsuccessorin 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.voidvoidvoidaddNode(int index, DoublyLinkedList.ListNode<E> node) Inserts the specifiednodeat the specified position in this list.voidaddNodeBefore(DoublyLinkedList.ListNode<E> node, DoublyLinkedList.ListNode<E> successor) Inserts the specifiednodebefore the specifiedsuccessorin this list.voidInserts the specifiednodeat the front of this list.voidInserts the specifiednodeat the end of this list.voidappend(DoublyLinkedList<E> movedList) Appends themovedListto the end of this list.circularIterator(E firstElement) Returns aDoublyLinkedList.NodeIteratorthat starts at the firstDoublyLinkedList.ListNodeof this list that is equal to the specifiedfirstElement, iterates in forward direction over the end of this list until the first node.voidclear()booleanReturns true if thisDoublyLinkedListcontains the specifiedDoublyLinkedList.ListNode.element()get(int index) getFirst()Returns the firstnodeof this list.getLast()Returns the lastnodeof this list.getNode(int index) Returns thenodeat the specified position in this list.intReturns the index of the specifiednodein this list, or -1 if this list does not contain thenode.voidinvert()Inverts the list.booleanisEmpty()iterator()lastNodeOf(Object element) Returns the lastnodeholding the specifiedelementin this list.listIterator(int index) listIterator(E element) Returns aDoublyLinkedList.ListNodeIteratorover the elements in this list (in proper sequence) starting with the firstDoublyLinkedList.ListNodewhose value is equal to the specifiedelement.voidmoveFrom(int index, DoublyLinkedList<E> movedList) Moves allListNodesof the givensourceListto this list and inserts them all before the node previously at the given position.Returns the firstnodeholding the specifiedelementin this list.booleanbooleanofferFirst(E e) booleanpeek()peekLast()poll()pollLast()pop()voidprepend(DoublyLinkedList<E> movedList) Prepends themovedListto the beginning of this list.voidremove()remove(int index) booleanbooleanbooleanremoveNode(DoublyLinkedList.ListNode<E> node) Removes thenodefrom this list.reverseCircularIterator(E firstElement) Returns aDoublyLinkedList.NodeIteratorthat starts at the firstDoublyLinkedList.ListNodeof this list that is equal to the specifiedfirstElement, iterates in reverse direction over the end of this list until the first node.intsize()Methods inherited from class java.util.AbstractSequentialList
addAll, setMethods inherited from class java.util.AbstractList
add, equals, hashCode, indexOf, lastIndexOf, removeRange, subListMethods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods 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:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>
-
size
public int size() -
clear
public void clear()- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
addNode
Inserts the specifiednodeat 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
nodeas first or last takes only constant time O(1).- Parameters:
index- index at which the specifiednodeis to be insertednode- the node to add- Throws:
IndexOutOfBoundsException- if the index is out of range (index < 0 || index > size())IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListNullPointerException- ifnodeisnull
-
addNodeFirst
Inserts the specifiednodeat the front of this list.This method has constant runtime complexity O(1).
- Parameters:
node- the node to add- Throws:
IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListNullPointerException- ifnodeisnull
-
addNodeLast
Inserts the specifiednodeat the end of this list.This method has constant runtime complexity O(1).
- Parameters:
node- the node to add- Throws:
IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListNullPointerException- ifnodeisnull
-
addNodeBefore
public void addNodeBefore(DoublyLinkedList.ListNode<E> node, DoublyLinkedList.ListNode<E> successor) Inserts the specifiednodebefore the specifiedsuccessorin this list.This method has constant runtime complexity O(1).
- Parameters:
node- the node to addsuccessor-ListNodebefore which thenodeis inserted- Throws:
IllegalArgumentException- ifnodeis already contained in this or anotherDoublyLinkedListorsuccessoris not contained in this listNullPointerException- ifsuccessorornodeisnull
-
getFirstNode
Returns the firstnodeof this list.This method has constant runtime complexity O(1).
- Returns:
- the first
ListNodeof this list - Throws:
NoSuchElementException- if this list is empty
-
getLastNode
Returns the lastnodeof this list.This method has constant runtime complexity O(1).
- Returns:
- the last
ListNodeof this list - Throws:
NoSuchElementException- if this list is empty
-
getNode
Returns thenodeat the specified position in this list.This method has linear runtime complexity O(n).
- Parameters:
index- index of theListNodeto return- Returns:
- the
ListNodeat 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 specifiednodein this list, or -1 if this list does not contain thenode.More formally, returns the index
isuch thatnode == getNode(i), or -1 if there is no such index. Because aListNodeis 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
nodebut returns in constant time O(1) ifnodeis notcontainedin this list.- Parameters:
node- the node to search for- Returns:
- the index of the specified
nodein this list, or -1 if this list does not containnode - Throws:
NullPointerException- ifnodeisnull
-
containsNode
Returns true if thisDoublyLinkedListcontains the specifiedDoublyLinkedList.ListNode.This method has constant runtime complexity O(1).
- Parameters:
node- the node whose presence in thisDoublyLinkedListis to be tested- Returns:
- true if this
DoublyLinkedListcontains theDoublyLinkedList.ListNode - Throws:
NullPointerException- ifnodeisnull
-
removeNode
Removes thenodefrom this list. Returns true ifnodewas in this list and is now removed. Ifnodeis 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- ifnodeisnull
-
nodeOf
Returns the firstnodeholding the specifiedelementin this list. More formally, returns the firstListNodesuch thatObjects.equals(element, node.getValue()), ornullif there is no such node.This method has linear runtime complexity O(n).
- Parameters:
element- the element whoseListNodeis to return- Returns:
- the first
ListNodeholding theelementor null if no node was found
-
lastNodeOf
Returns the lastnodeholding the specifiedelementin this list. More formally, returns the lastListNodesuch thatObjects.equals(element, node.getValue()), ornullif there is no such node.This method has linear runtime complexity O(n).
- Parameters:
element- the element whoseListNodeis to return- Returns:
- the last
ListNodeholding theelementor null if no node was found
-
addElementFirst
Inserts the specified element at the front of this list. Returns theDoublyLinkedList.ListNodeallocated to store thevalue. The returnedListNodeis 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
ListNodeallocated to store thevalue
-
addElementLast
Inserts the specified element at the end of this list. Returns theDoublyLinkedList.ListNodeallocated to store thevalue. The returnedListNodeis 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
ListNodeallocated to store thevalue
-
addElementBeforeNode
public DoublyLinkedList.ListNode<E> addElementBeforeNode(DoublyLinkedList.ListNode<E> successor, E element) Inserts the specified element before the specifiedsuccessorin this list. Returns theListNodeallocated to store thevalue.- Parameters:
successor-ListNodebefore which the node holdingvalueis insertedelement- the element to add- Returns:
- the
ListNodeallocated to store thevalue - Throws:
IllegalArgumentException- ifsuccessoris not contained in this listNullPointerException- ifsuccessorisnull
-
add
-
get
-
remove
-
addFirst
-
addLast
-
offerFirst
- Specified by:
offerFirstin interfaceDeque<E>
-
offerLast
-
removeFirst
- Specified by:
removeFirstin interfaceDeque<E>
-
removeLast
- Specified by:
removeLastin interfaceDeque<E>
-
pollFirst
-
pollLast
-
getFirst
-
getLast
-
peekFirst
-
peekLast
-
removeFirstOccurrence
- Specified by:
removeFirstOccurrencein interfaceDeque<E>
-
removeLastOccurrence
- Specified by:
removeLastOccurrencein 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 allListNodesof the givensourceListto this list and inserts them all before the node previously at the given position. All thenodesofmovedListare moved to this list. When this method terminates this list contains all nodes ofmovedListandmovedListis empty.- Parameters:
index- index of the first element oflistin thislistafter it was addedmovedList- theDoublyLinkedListto move to this one- Throws:
NullPointerException- ifmovedListisnull
-
append
Appends themovedListto the end of this list. All the elements frommovedListare transferred to this list, i.e. thelistis empty after calling this method.- Parameters:
movedList- theDoublyLinkedListto append to this one- Throws:
NullPointerException- ifmovedListisnull
-
prepend
Prepends themovedListto the beginning of this list. All the elements frommovedListare transferred to this list, i.e. themovedListis empty after calling this method.- Parameters:
movedList- theDoublyLinkedListto prepend to this one- Throws:
NullPointerException- ifmovedListisnull
-
circularIterator
Returns aDoublyLinkedList.NodeIteratorthat starts at the firstDoublyLinkedList.ListNodeof 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 firstnodethat holds a value such thatObjects.equals(node.getValue, firstElement)returnstrue. The returnedNodeIteratoriterates in forward direction returning the respective next element in subsequent calls tonext(Node). The returned iterator ignores the actual bounds of thisDoublyLinkedListand iterates until the node before the first one is reached. ItshasNext()returnsfalseif the next node would be the first one.- Parameters:
firstElement- the element equal to the firstnext()- Returns:
- a circular
NodeIteratoriterating forward fromfirstElement
-
reverseCircularIterator
Returns aDoublyLinkedList.NodeIteratorthat starts at the firstDoublyLinkedList.ListNodeof 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 firstnodethat holds a value such thatObjects.equals(node.getValue, firstElement)returnstrue. The returnedNodeIteratoriterates in reverse direction returning the respective previous element in subsequent calls tonext(Node). The returned iterator ignores the actual bounds of thisDoublyLinkedListand iterates until the node before the first one is reached. ItshasNext()returnsfalseif the next node would be the first one.- Parameters:
firstElement- the element equal to the firstnext()- Returns:
- a circular
NodeIteratoriterating backwards fromfirstElement
-
descendingIterator
- Specified by:
descendingIteratorin interfaceDeque<E>
-
iterator
-
listIterator
- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>
-
listIterator
- Specified by:
listIteratorin interfaceList<E>- Specified by:
listIteratorin classAbstractSequentialList<E>
-
listIterator
Returns aDoublyLinkedList.ListNodeIteratorover the elements in this list (in proper sequence) starting with the firstDoublyLinkedList.ListNodewhose value is equal to the specifiedelement.- Parameters:
element- the first element to be returned from the list iterator (by a call to thenextmethod)- Returns:
- a list iterator over the elements in this list (in proper sequence)
- Throws:
NoSuchElementException- ifelementis not in the list
-