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:
java.lang.Iterable<E>,java.util.Collection<E>,java.util.Deque<E>,java.util.List<E>,java.util.Queue<E>
public class DoublyLinkedList<E> extends java.util.AbstractSequentialList<E> implements java.util.Deque<E>DoublyLinkedListimplements a doubly linkedListdata structure, that exposes itsListNodeswhere the data is stored in.An element holding
ListNodecan be removed or added to aDoublyLinkedListin constant time O(1). Other methods that operate onListNodesdirectly 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
DoublyLinkedListsupportsnullelements 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
ConcurrentModificationExceptionafter 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 theListNodesof thisListare accessible and can be removed or added directly. To ensure the integrity of theListnodes of this List have a reference to the List they belong to. This increases the memory occupied by this list implementation compared toLinkedListfor the same elements. Instances ofLinkedList.Nodehave three references each (the element, next and previous), instances ofDoublyLinkedList.ListNodehave four (the element, next, previous and the list).- Author:
- Timofey Chudakov, Hannes Wellmann
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceDoublyLinkedList.ListNode<V>Container for the elements stored in aDoublyLinkedList.static interfaceDoublyLinkedList.ListNodeIterator<E>static interfaceDoublyLinkedList.NodeIterator<E>
-
Constructor Summary
Constructors Constructor Description DoublyLinkedList()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E element)DoublyLinkedList.ListNode<E>addElementBeforeNode(DoublyLinkedList.ListNode<E> successor, E element)Inserts the specified element before the specifiedsuccessorin 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.voidaddFirst(E e)voidaddLast(E e)voidaddNode(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.voidaddNodeFirst(DoublyLinkedList.ListNode<E> node)Inserts the specifiednodeat the front of this list.voidaddNodeLast(DoublyLinkedList.ListNode<E> node)Inserts the specifiednodeat the end of this list.voidappend(DoublyLinkedList<E> movedList)Appends themovedListto the end of this list.DoublyLinkedList.NodeIterator<E>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()booleancontainsNode(DoublyLinkedList.ListNode<E> node)Returns true if thisDoublyLinkedListcontains the specifiedDoublyLinkedList.ListNode.DoublyLinkedList.NodeIterator<E>descendingIterator()Eelement()Eget(int index)EgetFirst()DoublyLinkedList.ListNode<E>getFirstNode()Returns the firstnodeof this list.EgetLast()DoublyLinkedList.ListNode<E>getLastNode()Returns the lastnodeof this list.DoublyLinkedList.ListNode<E>getNode(int index)Returns thenodeat the specified position in this list.intindexOfNode(DoublyLinkedList.ListNode<E> node)Returns the index of the specifiednodein this list, or -1 if this list does not contain thenode.voidinvert()Inverts the list.booleanisEmpty()DoublyLinkedList.NodeIterator<E>iterator()DoublyLinkedList.ListNode<E>lastNodeOf(java.lang.Object element)Returns the lastnodeholding the specifiedelementin this list.DoublyLinkedList.ListNodeIterator<E>listIterator()DoublyLinkedList.ListNodeIterator<E>listIterator(int index)DoublyLinkedList.ListNodeIterator<E>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.DoublyLinkedList.ListNode<E>nodeOf(java.lang.Object element)Returns the firstnodeholding the specifiedelementin this list.booleanoffer(E e)booleanofferFirst(E e)booleanofferLast(E e)Epeek()EpeekFirst()EpeekLast()Epoll()EpollFirst()EpollLast()Epop()voidprepend(DoublyLinkedList<E> movedList)Prepends themovedListto the beginning of this list.voidpush(E e)Eremove()Eremove(int index)EremoveFirst()booleanremoveFirstOccurrence(java.lang.Object o)EremoveLast()booleanremoveLastOccurrence(java.lang.Object o)booleanremoveNode(DoublyLinkedList.ListNode<E> node)Removes thenodefrom this list.DoublyLinkedList.NodeIterator<E>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.AbstractList
add, equals, hashCode, indexOf, lastIndexOf, removeRange, subList
-
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, remove, removeAll, retainAll, toArray, toArray, toString
-
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
-
size
public int size()
-
clear
public void clear()
-
addNode
public void addNode(int index, DoublyLinkedList.ListNode<E> node)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:
java.lang.IndexOutOfBoundsException- if the index is out of range (index < 0 || index > size())java.lang.IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListjava.lang.NullPointerException- ifnodeisnull
-
addNodeFirst
public void addNodeFirst(DoublyLinkedList.ListNode<E> node)
Inserts the specifiednodeat the front of this list.This method has constant runtime complexity O(1).
- Parameters:
node- the node to add- Throws:
java.lang.IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListjava.lang.NullPointerException- ifnodeisnull
-
addNodeLast
public void addNodeLast(DoublyLinkedList.ListNode<E> node)
Inserts the specifiednodeat the end of this list.This method has constant runtime complexity O(1).
- Parameters:
node- the node to add- Throws:
java.lang.IllegalArgumentException- ifnodeis already part of this or anotherDoublyLinkedListjava.lang.NullPointerException- 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:
java.lang.IllegalArgumentException- ifnodeis already contained in this or anotherDoublyLinkedListorsuccessoris not contained in this listjava.lang.NullPointerException- ifsuccessorornodeisnull
-
getFirstNode
public DoublyLinkedList.ListNode<E> getFirstNode()
Returns the firstnodeof this list.This method has constant runtime complexity O(1).
- Returns:
- the first
ListNodeof this list - Throws:
java.util.NoSuchElementException- if this list is empty
-
getLastNode
public DoublyLinkedList.ListNode<E> getLastNode()
Returns the lastnodeof this list.This method has constant runtime complexity O(1).
- Returns:
- the last
ListNodeof this list - Throws:
java.util.NoSuchElementException- if this list is empty
-
getNode
public DoublyLinkedList.ListNode<E> getNode(int index)
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:
java.lang.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 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:
java.lang.NullPointerException- ifnodeisnull
-
containsNode
public boolean containsNode(DoublyLinkedList.ListNode<E> node)
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:
java.lang.NullPointerException- ifnodeisnull
-
removeNode
public boolean removeNode(DoublyLinkedList.ListNode<E> node)
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:
java.lang.NullPointerException- ifnodeisnull
-
nodeOf
public DoublyLinkedList.ListNode<E> nodeOf(java.lang.Object element)
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
public DoublyLinkedList.ListNode<E> lastNodeOf(java.lang.Object element)
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
public DoublyLinkedList.ListNode<E> addElementFirst(E element)
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
public DoublyLinkedList.ListNode<E> addElementLast(E element)
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:
java.lang.IllegalArgumentException- ifsuccessoris not contained in this listjava.lang.NullPointerException- ifsuccessorisnull
-
add
public void add(int index, E element)
-
get
public E get(int index)
-
remove
public E remove(int index)
-
removeFirstOccurrence
public boolean removeFirstOccurrence(java.lang.Object o)
- Specified by:
removeFirstOccurrencein interfacejava.util.Deque<E>
-
removeLastOccurrence
public boolean removeLastOccurrence(java.lang.Object o)
- Specified by:
removeLastOccurrencein interfacejava.util.Deque<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 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:
java.lang.NullPointerException- ifmovedListisnull
-
append
public void append(DoublyLinkedList<E> movedList)
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:
java.lang.NullPointerException- ifmovedListisnull
-
prepend
public void prepend(DoublyLinkedList<E> movedList)
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:
java.lang.NullPointerException- ifmovedListisnull
-
circularIterator
public DoublyLinkedList.NodeIterator<E> 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.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
public DoublyLinkedList.NodeIterator<E> 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.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
public DoublyLinkedList.NodeIterator<E> descendingIterator()
- Specified by:
descendingIteratorin interfacejava.util.Deque<E>
-
iterator
public DoublyLinkedList.NodeIterator<E> iterator()
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> listIterator()
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> listIterator(int index)
-
listIterator
public DoublyLinkedList.ListNodeIterator<E> 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.- 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:
java.util.NoSuchElementException- ifelementis not in the list
-
-