1. IntroductionThe LinkedList class declares the following:
Public class extends Implements List<e>, Deque<e>, Cloneable, java.io.Serializable
It can be found that LinkedList inherits the Abstractsequentiallist abstract class, rather than implementing abstractlist like ArrayList and vector, in fact, the Java class Library has only LinkedList inherits this abstract class, as its name provides, an abstraction of successive accesses to a sequence:the bottom of the LinkedList is the deque doubly linked list, implements the Deque interface, and the Deque interface inherits from the queue interface, so in Java, if you want to implement the queue, generally use LinkedList to implement.
node node.Node nodes have three properties: item represents the node value, prev represents the node's previous node, and next represents the latter node of the node. the definition of a linked list node in LinkedList is as follows:
Private Static class Node<e> { E item; Node<E> next; Node<E> prev; Node (node<E> prev, E element, node<e> next) { this. Item= element; this. Next = next; this. prev = prev; } }
each node has a precursor and successor node, and two variables are defined in LinkedList to point to the first and last node in the list, respectively:
transient node<e> first ; transient Node<e> last;
2. Adding (add) Operation
Public BooleanAdd (e e) {linklast (e); return true;}voidLinklast (e e) {FinalNode<e> L = last;//The last node of a linked list FinalNode<e> NewNode =NewNode<> (L, E,NULL);//The node object is established, the previous (Perv property) is last, the latter (next property) is null, and the middle item data is the input parameter eLast =NewNode; if(L = =NULL) First= NewNode;//if the last node is null to indicate that the linked list is empty, the new data is added as the first node when adding data ElseL.next= NewNode;//If the list is not empty, the next property of the last linked list points to the newly added nodesize++;//linked list length self-increment 1modcount++;}Private Static classNode<e>{E item; Node<E>Next; Node<E>prev; Node (node<E> prev, E element, node<e>next) { This. Item =element; This. Next =Next; This. prev =prev; }}
Summary:LinkedList Each newly added object is placed into the new node object when the add operation is added, the node object contains the joined object and the forward and backward pointer properties (PRED and next,pred and next are actually node objects, The direct storage is the previous node object of the current object and the latter node object, the last one and the next value of the node is null, and then the original last node's next pointer to the newly added object, you can
AddAll Operation:
Public BooleanAddAll (intindex, COLLECTION<?extendsE>c) {checkpositionindex (index);//determine if index is out of bounds and out of bounds throws an exceptionObject[] A =C.toarray (); intNumnew = A.length;//The length of the collection to insert if(Numnew = = 0) return false; Node<E> pred, SUCC;//declares pred and succ two node objects that identify the previous node and the last node to insert the element if(index = = size) {//if size equals the length of the original array, it is added at the endSUCC =NULL; Pred=Last ; } Else{succ= node (index);//node object at index positionPred =Succ.prev; } for(Object o:a) {//traverse the collection to be inserted@SuppressWarnings ("unchecked") e E =(E) o; Node<E> NewNode =NewNode<> (Pred, E,NULL); if(Pred = =NULL) First= NewNode;//if the previous node of the position to be inserted is a null representation of the first node, the NewNode is assigned directly to the first node ElsePred.next= NewNode;//assigns the collection element node object that is to be inserted to the last object of the original node object in this position, that is, the next pointer of the previous node object is changed to the newly inserted nodepred = NewNode;//after the change point, assign the new node object to pred as the previous object node of the new insertion node in the next loop, looping } //at this point pred represents the last node object after the insertion of the collection element if(SUCC = =NULL) {//end adds the node object of the last collection after the collection element is added pred asLast =pred; } Else{Pred.next= SUCC;//The next pointer to the last node object of the collection element points to the node object at the original index positionSucc.prev = pred;//points The Pred pointer object on the original index position to the last object of the collection} size+=numnew; Modcount++; return true;} Node<E> node (intindex) { if(Index < (size >> 1)) {//determines whether the index is less than half of the size, or if it is less than the node from the beginning, otherwise traverses the node from the endnode<e> x =First ; for(inti = 0; I < index; i++) x= X.next;//start with the first node, and then assign the next node to the X returnX//returns the Node object at the index position,}Else{Node<E> x =Last ; for(inti = size-1; i > Index; i--) x=X.prev; returnx; }}
Summary:LinkedList Inserts an element at a location by pointing to the newly inserted element with the last pointer to the previous node of the original position node, and then by pointing the previous pointer to the new element of the node in the original position, which is equivalent to moving the element behind the element after it has been inserted, But it's not like ArrayList to move the elements behind all the insertion positions back, just to change the point of the front and back nodes.
3. Delete Operation
PublicE Remove (intindex) {Checkelementindex (index); returnUnlink (node (index));} E Unlink (Node<E>x) {FinalE element =X.item; FinalNode<e> next =X.next; FinalNode<e> prev =X.prev; if(prev = =NULL) { First=Next; } Else{Prev.next= Next;//Assigns the next node object of the incoming node to the next node object in its previous node , skipping the incoming node objectX.prev =NULL;//empty the previous node object of the incoming object so that its previous pointer does not point to any element } if(Next = =NULL) { last= prev;//If Next is the null representation of the last element, assign the pred node directly to}Else{Next.prev=prev; X.next=NULL;//empty the latter node of the incoming node so that a node pointer does not point to any element} x.item=NULL;//empty the objects on the incoming node object, that is, the properties in the entire node object are empty, followed by GC recyclingsize--; Modcount++; returnelement;}
Summary:The LinkedList delete operation is done by placing the next node at the index position at the last node of the index position, pointing the pred of the latter node to the previous node, and then emptying the node object properties at the index location. The empty node object is reclaimed by the GC garbage collection period.
4, modify the Operation
Public E set (int index, e Element) { Checkelementindex (index); // Check index out of bounds Node<e> x = node (index); // Gets the Node object on the index position E oldval = x.item; = element; // assign the newly inserted element directly to the Item property of the node object at this location return Oldval;}
Summary:the actual data in the LinkedList is saved in the Item property of the Node object
5. Query Operation
Public E get (int index) { checkelementindex (index); // Check index out of bounds return node (index). Item; // returns the Item property of the node object at the index position, which is the actual value at that location }
Summary:The socialize operation is implemented through the node method, and the node method is determined by first half or the last half of the entire list to get the element on the index position.
6, LinkedList and ArrayList of the general difference:1, ArrayList inherit in Abstractlist, LinkedList inherit from Abstractsequentiallist;2, ArrayList based on the array, LinkedList based on a doubly linked list, for random access, ArrayList comparative advantage, linkedlist Insert, delete elements faster, if only adjust the pointer to the point so time complexity is O (1), However, if traversal is required for a particular location, the time complexity is O (n), which means that the linkedlist is relatively slow at random access to the element;3, LinkedList did not realize their own Iterator, but there are listiterator and descendingiterator;4, LinkedList need more memory, because ArrayList each index location is the actual data, and LinkedList in each node is stored in the actual data and the location of the front and back nodes;5, ArrayList and LinkedList are non-synchronous collections. 6, like ArrayList, LinkedList is also non-thread-safe, only in a single thread can be used. To prevent non-synchronous access, you can create Linkedlist:list list= collections.synchronizedlist (New LinkedList ()) as follows:7, LinkedList based on two-way linked list implementation, elements can be null.
LinkedList Source Code Analysis