I. Internal data structure of LinkedList
LinkedList Bottom is a linked list of data structure, using a doubly linked list, the basic node data structure code is as follows:
Private Static classNode<e>{E item; //elements placed by nodesNode<e> Next;//Next NodeNode<e> prev;//Previous NodeNode (node<E> prev, E element, node<e>next) { This. Item =element; This. Next =Next; This. prev =prev; } }
Two. Common operations for LinkedList nodes
In the LinkedList implementation of a series of methods of the list interface, the bottom is through a series of node operations to complete the implementation of the LinkedList method, the operation code is as follows:
/*** Link an element to the beginning of a linked list*/ Private voidLinkfirst (e e) {FinalNode<e> f =First ; FinalNode<e> NewNode =NewNode<> (NULL, E, f); First=NewNode; if(f = =NULL) Last=NewNode; ElseF.prev=NewNode; Size++; Modcount++; } /*** LINK element e to the end of the list*/ voidLinklast (e e) {Finalnode<e> L =Last ; FinalNode<e> NewNode =NewNode<> (L, E,NULL); Last=NewNode; if(L = =NULL) First=NewNode; ElseL.next=NewNode; Size++; Modcount++; } /*** Link an element to the front of the specified node*/ voidLinkbefore (e E, node<e>succ) { //assert succ! = null; Finalnode<e> pred =Succ.prev; FinalNode<e> NewNode =NewNode<>(Pred, E, succ); Succ.prev=NewNode; if(Pred = =NULL) First=NewNode; ElsePred.next=NewNode; Size++; Modcount++; } /*** Disassociate a first node*/ PrivateE Unlinkfirst (node<e>f) {FinalE element =F.item; FinalNode<e> next =F.next; F.item=NULL; F.next=NULL;//Help GCFirst =Next; if(Next = =NULL) Last=NULL; ElseNext.prev=NULL; Size--; Modcount++; returnelement; } /*** Disassociate a last node*/ PrivateE Unlinklast (node<e>l) {FinalE element =L.item; FinalNode<e> prev =L.prev; L.item=NULL; L.prev=NULL;//Help GCLast =prev; if(prev = =NULL) First=NULL; ElsePrev.next=NULL; Size--; Modcount++; returnelement; } /*** Unlink a node x*/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; X.prev=NULL; } if(Next = =NULL) { last=prev; } Else{Next.prev=prev; X.next=NULL; } X.item=NULL; Size--; Modcount++; returnelement; }
Three. AddAll method of LinkedList
When inserting an element of the collection type, you need to convert the collection element to an element of the array type, then iterate over each element in the arrays, establish a series of nodes, connect the nodes to each other, and then connect with the original nodes.
Public BooleanAddAll (intindex, COLLECTION<?extendsE>c) {checkpositionindex (index); Object[] a=C.toarray (); intNumnew =a.length; if(Numnew = = 0) return false; Node<E>pred, SUCC; if(index = = size) {//indicates that the end of the list is addedSUCC =NULL; Pred=Last ; } Else{//add in the middle of a linked listSUCC = node (index);//gets the element at the current index positionpred = Succ.prev;//pred--an element in front of the element that records the current index position } for(Object o:a) {@SuppressWarnings ("Unchecked") e E =(E) o; Node<E> NewNode =NewNode<> (Pred, E,NULL);//adds an element before the element at the current index position if(Pred = =NULL)//there is no element in front, indicating that the inserted node is the first element of a linked listFirst =NewNode; Else //next of the previous node points to the current nodePred.next =NewNode; Pred= NewNode;//pred points to the current node to prepare for the next assignment } if(SUCC = =NULL) {//There are no elements at the index before insertingLast =pred; } Else{Pred.next=succ; Succ.prev=pred; } size+=numnew; Modcount++; return true; }
Four. Remove operation
The remove operation iterates through the linked table nodes, and if the element of the node equals the element of the parameter, the unlink method is called to dismiss the node relationship.
Public BooleanRemove (Object o) {if(O = =NULL) { for(node<e> x = first; X! =NULL; x =x.next) {if(X.item = =NULL) {unlink (x); return true; } } } Else { for(node<e> x = first; X! =NULL; x =x.next) {if(O.equals (X.item)) {unlink (x); return true; } } } return false; }
Five. Clear method
Pass to the garbage collector processing by iterating through all elements of the list, and setting all properties of the traversed element to null.
public void Clear () { for (node<e> x = First; X! = null <E> next = X.next; X.item = null ; X.next = null ; X.prev = null ; x = next; } First = last = null ; Size = 0; Modcount ++; }
Six. Get method
The Get method is a method that LinkedList gets the element at an index, because the data structure of the linked list is relatively slow to query, Therefore, it is generally not recommended to call a series of get methods for data structures of type LinkedList. However, the LinkedList data structure itself is optimized for the Get method, starting with the last element when the index is larger than the intermediate index. When the index is less than the intermediate index, Start the query from the first element.
PublicE Get (intindex) {Checkelementindex (index); returnnode (index). Item; }node<E> node (intindex) { if(Index < (size >> 1) ) {Node<E> x = First;//start querying from the first node. for(inti = 0; I < index; i++) x=X.next; returnx; } Else{Node<E> x = Last;//start querying from the end node for(inti = size-1; i > Index; i--) x=X.prev; returnx; } }
Java Source Beginner _linkedlist