Heart in Shandong body in Wu, floating Peng Jianghai diffuse sigh sigh.
If he then Lingyun Zhi, dare laugh Huang Chao not husband.
--Water Margin
First on the source code, LinkedList class:
1 Private Static classNode<e> {2 E Item;3Node<e>Next;4Node<e>prev;5 6Node (node<e> prev, E element, node<e>next) {7 This. Item =element;8 This. Next =Next;9 This. prev =prev;Ten } One}
The Java list defines an inner class node class, and "node" is the meaning of the node. The basic element of a linked list is a node, (doubly linked list) Each node contains three members, item: Data, Next: Pointer to the next element of the list, prev: pointer to the previous element
First look at the linked list in C:
The head pointer variable holds an address that points to a variable no.1,no.1 and holds a pointer to No.2, and so on until the address saved in no.x points to No.last, the last item in the diagram is No.3, which points to null.
The prev pointer is added to each element of the doubly linked list.
Two-way linked table diagram:
But there is no pointer to the memory address in Java, so how to implement the linked list?
Then look at the Java source code:
1TransientintSize =0;2 3 /**4 * Pointer to first node.5 * Invariant: (first = = NULL && last = = null) | |6 * (First.prev = = NULL && First.item! = null)7 */8Transient node<e>First ;9 Ten /** One * Pointer to the last node. A * Invariant: (first = = NULL && last = = null) | | - * (Last.next = = NULL && Last.item! = null) - */ theTransient node<e>Last ; - - /** - * Constructs an empty list. + */ - PublicLinkedList () { + } A at /** - * Constructs a list containing the elements of the specified - * collection, in the order they is returned by the collection ' s - * iterator. - * - * @param c the collection whose elements is to being placed into this list in * @throws NullPointerException If the specified collection is null - */ to PublicLinkedList (collection<? extends e>c) { + This(); - AddAll (c); the}
View Code
The LinkedList class defines two temporary nodes, first and last, and two constructors. A non-parametric construction and a parameter-constructed, non-parametric construction creates a list of linked lists, with a parameter construct that adds a whole set of links to the list
Let's take a look at the Add () method:
1 Public Boolean Add (E e) {2 linklast (e); 3 return true ; 4 }
1 voidLinklast (e e) {2Final node<e> L =Last ;3Final node<e> NewNode =NewNode<> (L, E,NULL);4Last =NewNode;5 if(L = =NULL)6First =NewNode;7 Else8L.next =NewNode;9size++;Tenmodcount++; One}
Drawing:
The initial list is empty: Prev and Next are both Null,item E, assuming that the incoming data is no.1,no.2 ...;
Now add the element:
Add a third element:
Add a FOURTH element:
etc...
It is visible that a linked list in Java is a reference variable pointing to a node instead of a pointer variable in C pointing to the memory address.
Each time you add a new element, you simply point the last element's next to an instance of the newly added element, and the prev of the newly added element points to the instance of the original last element.
Then look at the Add (int index, E Element) method:
1 Public void Add (int Index, E Element) {2 checkpositionindex (index); 3 4 if (Index = = size) 5 linklast (element); 6 Else 7 Linkbefore (element, node (index)); 8 }
1Node<e> node (intindex) {2 //assert Iselementindex (index);3 4 if(Index < (size >>1)) {5node<e> x =First ;6 for(inti =0; I < index; i++)7x =X.next;8 returnx;9}Else {Tennode<e> x =Last ; One for(inti = size-1; i > Index; i--) Ax =X.prev; - returnx; - } the}
This assumes that the incoming index is smaller than size,
Node (int index) returns the node instance with position index in a very simple way, then executes the Linkbefore (e E, node<e> succ) method
1 voidLinkbefore (e E, node<e>succ) {2 //assert succ! = null;3Final node<e> pred =Succ.prev;4Final node<e> NewNode =NewNode<>(Pred, E, succ);5Succ.prev =NewNode;6 if(Pred = =NULL)7First =NewNode;8 Else9Pred.next =NewNode;Tensize++; Onemodcount++; A}
The NewNode prev points to the Prev node of the original position node, and next points to the original position node;
The prev of the original position node is pointed to NewNode;
The next point of the Prev node (if any) of the original position node is pointed to newnode; if not, the NewNode is the first node and the original position node becomes the second node;
Other linked list methods are omitted here ... The principle is the same.
Java Learning notes-linked list