LinkedList Introduction
LinkedList is a doubly linked list that inherits from the Abstractsequentiallist. It can also be manipulated as a stack, a queue, or a two-terminal queue.
LinkedList implements the List interface and can queue operations on it.
LinkedList implements the Deque interface, that is, LinkedList can be used as a two-terminal queue.
LinkedList implements the Cloneable interface, which covers the function clone () and can be cloned.
LinkedList implements the Java.io.Serializable interface, which means that LinkedList supports serialization and can be transmitted through serialization.
LinkedList is not synchronized.
LinkedList relative to ArrayList, it is possible to quickly add, delete elements, ArrayList add delete elements to move the elements of the array, you may also need to consider the expansion of the array length. LinkedList Property The linkedlist itself has fewer properties, mainly three, one is size, the table name currently has a number of nodes, one is the first node, and the last one represents the final node.
public class linkedlist<e>
extends abstractsequentiallist<e>
implements List<e>, Deque <e>, cloneable, java.io.serializable{
//Current number of nodes
transient int size = 0;
First node
transient node<e>;
The last node
transient node<e>;
Omit internal classes and methods.
}
LinkedList Construction Method
The default construction method is empty and nothing is done, indicating that the node size 0,first and last is null when initialized:
Public LinkedList () {
}
Another construction method is a constructor with a collection worthy object as an entry parameter, and the following is the execution logic:
1 Use this () to invoke the default parameterless constructor.
2 calls the AddAll () method, passing in the current number of nodes, the size is 0, and the collection object is passed in
3 Check the index has no array of suspects
4 Convert collection to array object a
5 loops through the A array, and then creates the elements in the a array into the nodes that have the back and forth connections, and then connects them in order.
6) Modify the value of the current number of node size
7 The Operation times Modcount from 1.
Here is the source code for the implementation:
Default constructor
Public LinkedList (COLLECTION<, extends e> c) {this
();
AddAll (c);
}
Calling the AddAll method with parameters
public boolean AddAll (collection<. extends e> c) {return
addall (size, c);
}
Convert a Collection object to an array list
public boolean addall (int index, COLLECTION<? extends e> c) {
checkpositionindex (index);
Object[] A = C.toarray ();
int numnew = a.length;
if (numnew = = 0) return
false;
Node<e> pred, succ;
if (index = = size) {
succ = null;
pred = last;
} else {
SUCC = node (index);
pred = Succ.prev;
}
for (Object o:a) {
@SuppressWarnings ("unchecked") E = (e) o;
node<e> NewNode = new Node<> (pred, E, null);
if (pred = = null)-
newNode;
else
pred.next = NewNode;
pred = NewNode;
}
if (succ = = null) {Last
= pred;
} else {
pred.next = succ;
Succ.prev = pred;
}
Size + = Numnew;
modcount++;
return true;
}
Add Method
Add (E E) method
This method directly puts the new elements in the last side of the list, then the length of the list (size) plus 1, the number of changes (Modcount) plus 1
Public boolean Add (e) {
linklast (e);
return true;
}
void Linklast (e e) {
final node<e> l = last;
Final node<e> NewNode = new node<> (l, E, null);
last = NewNode;
if (L = = null),
NewNode;
else
l.next = NewNode;
size++;
modcount++;
}
Add (int index, E Element) method
Add an element to an array list at the specified location
1 Check added location index is not less than equal to the current length of the list size, and requires greater than 0
2 if the index is equal to size, then directly to the end of the list to add elements, equivalent to calling the Add (E E) method
3 If index is not equal to size, first index to the element at index position, then add the new element before the position of index.
public void Add (int index, E element) {
Checkpositionindex (index);
if (index = = size)
linklast (element);
else
Linkbefore (element, node (index));
After adding the indexed element to the new element
void Linkbefore (e E, node<e> succ) {
//assert succ!= null;
Final node<e> pred = Succ.prev;
Final node<e> NewNode = new Node<> (pred, E, succ);
Succ.prev = NewNode;
if (pred = = null)-
newNode;
else
pred.next = NewNode;
size++;
modcount++;
}
Get Method
Get MethodThe first is to determine if the index position is out of bounds, after determining the elements that begin traversing the list after completion, do you want to iterate from the beginning or from the end, where the index is compared to half the length of the current list, and if the index position is less than half the length of the current list, the end is traversed
Public E get (int index) {
checkelementindex (index);
Return node (index). Item;
Traversing linked list elements
node<e> node (int index) {
//Assert Iselementindex (index);
if (Index < (size >> 1)) {
node<e> x = i;
for (int i = 0; i < index; i++)
x = X.next;
return x;
} else {
node<e> x = last;
for (int i = size-1 i > index; i--)
x = X.prev;
return x;
}
}
GetFirst MethodReturn the first element directly
Public E GetFirst () {
final node<e> f = i;
if (f = = null)
throw new Nosuchelementexception ();
return f.item;
}
GetLast Method
Return the last element directly
Public E-GetLast () {
final node<e> l = last;
if (L = = null)
throw new Nosuchelementexception ();
return l.item;
}
Remove Method Remove () method
The nature of the Remove method calls or Removefirst method
Public E-Remove () {return
removefirst ();
}
Removefirst () method
The first node is removed, the first node is empty, the next node becomes the first node, the list length is reduced by 1, the number of modifications is 1, and the first node that is removed is returned.
Public E Removefirst () {
final node<e> f = i;
if (f = = null)
throw new Nosuchelementexception ();
Return Unlinkfirst (f);
}
Private E Unlinkfirst (node<e> f) {
//assert F = = i && f!= null;
Final E element = F.item;
Final node<e> next = F.next;
F.item = null;
F.next = null; Help GC-I
= next;
if (next = null) last
= null;
else
next.prev = null;
size--;
modcount++;
return element;
}
removelast () method
Remove the last node, put the last node empty, the last node of the final node into the previous node, the chain table length minus 1, modify the number plus 1, to return the last node removed.
Public E-Removelast () {
final node<e> l = last;
if (L = = null)
throw new Nosuchelementexception ();
Return Unlinklast (L);
}
Private E Unlinklast (node<e> l) {
//assert L = = last && l!= null;
Final E element = L.item;
Final node<e> prev = L.prev;
L.item = null;
L.prev = null; Help GC last
= prev;
if (prev = null) is a
= null;
else
prev.next = null;
size--;
modcount++;
return element;
}
The remove (int index) method first checks whether the removed position is within the range of the list length, if not, throws an exception, gets the node that needs to be removed, and then the removed node is empty to dock the last node and the next node.
Public E-Remove (int index) {
checkelementindex (index);
Return unlink (node (index));
Set Method
Check that the set element bit is then placed across bounds, and if not, the node indexed to the index position replaces the node contents of the index position with the new content element and returns the old value.
Public E Set (int index, E element) {
Checkelementindex (index);
Node<e> x = node (index);
E oldval = X.item;
X.item = element;
return oldval;
}
The clear method will empty all the linked list elements, and then modify the list length to 0, the number of changes plus 1
public void Clear () {
//clearing all of the links between nodes is "unnecessary", but:
//-Helps a generational GC if the discarded nodes inhabit
/More than one generation//-are sure to free
memory even if there is A reachable iterator for
(node<e> x = i x!= null;) {
node<e> next = x.next;
X.item = null;
X.next = null;
X.prev = null;
x = Next;
}
i = last = null;
size = 0;
modcount++;
}
Push and Pop methods
The push is actually called the AddFirst (e) method, and the pop calls the Removefirst () method.
The ToArray method creates an array object of an object, and then adds all nodes to the object object, returning the object array.
Public object[] ToArray () {
object[] result = new Object[size];
int i = 0;
for (node<e> x = i x!= null; x = x.next)
result[i++] = X.item;
return result;
}
Listiterator MethodThis method returns an internal class listiterator that the user can use the current list element of the internal class variable, but because LinkedList is also a non thread-safe class, and the ArrayList source analysis in the previous article (based on JDK8) Like iterator, multithreading can also be used under the exception of multithreaded modifications.
Public listiterator<e> listiterator (int index) {
checkpositionindex (index);
return new Listitr (index);
}