Linkedlist<e> Source Code Analysis

Source: Internet
Author: User

The LinkedList data structure is a doubly linked list, as follows:

Private Static class Node<e> {        E item;//data element        node <E> next;//successor node        <E> prev;// The Precursor node nodes        (node <E> prev, E element, node<e> next)            {this. Item =  element;             this. Next = next;             this. prev = prev;        }    }

Constructor:

int 0 ; //Data Count transient Node<E> first ;  Represents the first node of the linked list transientnode <E> last ;  Represents the last node   of the list public public linkedlist (COLLECTION<? extends e> c) { //For consolidating collection types of data           This ();        AddAll (c);}

Add

  PublicBoolean Add (E e) {linklast (e); return true; } voidLinklast (e e) {//using the end-interpolation method of final Node<E> L =Last ; Final Node<E> NewNode =NewNode<> (L, E,NULL); //The predecessor of the new node points to the last address, followed by null,
//So it's a doubly linked list, but not a loop, a loop, a successor pointing to the head node last =NewNode; //Let last point to the new node, and say this new node is the final element of the list
if(L = =NULL)//When first added, First,last is null if last is NULL, indicating that this is an empty list=NewNode; //Let the new node point to First, now the same node ElseL.next=NewNode; //When adding data, let the last node of the old list point to the new node (that node would have been null)
Size++; //Length plus 1 modcount++;
/**
Summarize:
Create a new node that points to the last node of the old list
The successor of the last node of the old list points to the new node
Make the new node the last node in the list
Length plus 1
First node precursor is NULL, last node is null

*/ }

Get

 PublicEGet(intindex) {Checkelementindex (index);//Check to see if the index is within the range of 0 to size        returnnode (index). Item; }node<E> node (intindex) {        //assert Iselementindex (index);        if(Index < (size >>1) {//See if the index is in the first half or the second half of the list, decide to search or search backwards, and then return to the row Node<E> x =First ;  for(inti =0; I < index; i++)//See the list here is the X starting from 0 =X.next; returnx; } Else{Node<E> x =Last ;  for(inti = size-1; i > Index; i--) x=X.prev; returnx; }    }

Remove

  PublicE Remove (intindex) {Checkelementindex (index);//Check the index firstreturnUnlink (node (index)); }//the index to find this node e unlink ( node<E>x) {//assert x! = null;Final E element =element of the x.item;//node final node<E> next =Subsequent final node of the x.next;//node<E> prev =precursor of the x.prev;//nodeif(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; }

Linkedlist<e> Source Code Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.