LinkedList

Doubly-linked list implementation of the {@code List} and {@code Deque} interfaces. Implements all optional list operations, and permits all elements (including {@code null}).

<p>All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

<p><strong>Note that this implementation is not synchronized.</strong> If multiple threads access a linked list concurrently, and at least one of the threads modifies the list structurally, it <i>must</i> be synchronized externally. (A structural modification is any operation that adds or deletes one or more elements; merely setting the value of an element is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the list.

If no such object exists, the list should be "wrapped" using the {@link Collections#synchronizedList Collections.synchronizedList} method. This is best done at creation time, to prevent accidental unsynchronized access to the list:<pre> List list = Collections.synchronizedList(new LinkedList(...));</pre>

<p>The iterators returned by this class's {@code iterator} and {@code listIterator} methods are <i>fail-fast</i>: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own {@code remove} or {@code add} methods, the iterator will throw a {@link ConcurrentModificationException}. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

<p>Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw {@code ConcurrentModificationException} on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: <i>the fail-fast behavior of iterators should be used only to detect bugs.</i>

<p>This class is a member of the <a href="{@docRoot}/../technotes/guides/collections/index.html"> Java Collections Framework</a>.

@author Josh Bloch @see List @see ArrayList @param E the type of elements held in this collection

Constructors

this
this()

Constructs an empty list.

this
this(Collection!E c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

this
this(E[] c)
Undocumented in source.

Members

Functions

add
bool add(E e)

Appends the specified element to the end of this list.

add
void add(int index, E element)

Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

addFirst
void addFirst(E e)

Inserts the specified element at the beginning of this list.

addLast
void addLast(E e)

Appends the specified element to the end of this list.

clear
void clear()

Removes all of the elements from this list. The list will be empty after this call returns.

contains
bool contains(E o)

Returns {@code true} if this list contains the specified element. More formally, returns {@code true} if and only if this list contains at least one element {@code e} such that <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.

descendingIterator
InputRange!E descendingIterator()
Undocumented in source. Be warned that the author may not have intended to support it.
element
E element()

Retrieves, but does not remove, the head (first element) of this list.

get
E get(int index)

Returns the element at the specified position in this list.

getFirst
E getFirst()

Returns the first element in this list.

getLast
E getLast()

Returns the last element in this list.

indexOf
int indexOf(E o)

Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index {@code i} such that <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>, or -1 if there is no such index.

lastIndexOf
int lastIndexOf(E o)

Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index {@code i} such that <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>, or -1 if there is no such index.

linkLast
void linkLast(E e)

Links e as last element.

offer
bool offer(E e)

Adds the specified element as the tail (last element) of this list.

offerFirst
bool offerFirst(E e)

Inserts the specified element at the front of this list.

offerLast
bool offerLast(E e)

Inserts the specified element at the end of this list.

opApply
int opApply(int delegate(ref E) dg)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(IObject o)
Undocumented in source. Be warned that the author may not have intended to support it.
opEquals
bool opEquals(Object o)
Undocumented in source. Be warned that the author may not have intended to support it.
peek
E peek()

Retrieves, but does not remove, the head (first element) of this list.

peekFirst
E peekFirst()

Retrieves, but does not remove, the first element of this list, or returns {@code null} if this list is empty.

peekLast
E peekLast()

Retrieves, but does not remove, the last element of this list, or returns {@code null} if this list is empty.

poll
E poll()

Retrieves and removes the head (first element) of this list.

pollFirst
E pollFirst()

Retrieves and removes the first element of this list, or returns {@code null} if this list is empty.

pollLast
E pollLast()

Retrieves and removes the last element of this list, or returns {@code null} if this list is empty.

pop
E pop()

Pops an element from the stack represented by this list. In other words, removes and returns the first element of this list.

push
void push(E e)

Pushes an element onto the stack represented by this list. In other words, inserts the element at the front of this list.

remove
bool remove(E o)

Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index {@code i} such that <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt> (if such an element exists). Returns {@code true} if this list contained the specified element (or equivalently, if this list changed as a result of the call).

remove
E remove()

Retrieves and removes the head (first element) of this list.

removeAt
E removeAt(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

removeFirst
E removeFirst()

Removes and returns the first element from this list.

removeFirstOccurrence
bool removeFirstOccurrence(E o)

Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). If the list does not contain the element, it is unchanged.

removeLast
E removeLast()

Removes and returns the last element from this list.

set
E set(int index, E element)

Replaces the element at the specified position in this list with the specified element.

size
int size()

Returns the number of elements in this list.

toArray
E[] toArray()

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

toHash
size_t toHash()
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.

Variables

_dlist
DList!E _dlist;
Undocumented in source.
_size
int _size;
Undocumented in source.

Meta