Implementation of java-single-link list

Source: Internet
Author: User

/*** Establish a single linked list * Create a one-link list *@authorFred **/ Public classSinglelinkedlist<e> {    PrivateNode<e>Head; PrivateE data; Private intsize; //Constructor     PublicSinglelinkedlist () {head=NewNode (); }        //get the size of the list//get the size of the list     Public intGetSize () {returnsize; }        //Insert data into the end of the list//add data to the linked list order     Public voidAdd (E data) {Node<E> node =NewNode (data,NULL); Node<E> temp =Head;  while(Temp.next! =NULL) {Temp=Temp.next; } Temp.next=node; Size++; }        //Insert data into the list at the special position//The location specified in the list index adds data     Public voidInsertbyindex (E data,intindex) {        if(Index < 1 | | index >size) {System.out.println ("Foot mark out of bounds"); return; } Node<E> node =NewNode (data,NULL); Node<E> temp =Head; intCount = 1;  while(Temp.next! =NULL) {            if(count++ = =index) {Node.next=Temp.next; Temp.next=node; Size++; return; }            Else{Temp=Temp.next; }        }            }        //Delete the data at a sepcial position//Delete data from a specified location in a linked list     Public voidDeletebyindex (intindex) {Node<E> temp =Head; intCount = 1;  while(Temp.next! =NULL) {            if(count++ = =index) {Temp.next=Temp.next.next; return; }            Else{Temp=Temp.next; }        }    }        //Clear the list//Empty List     Public voidClear () {size= 0; Head.next=NULL; } @Override Publicstring toString () {string result= "Data:"; Node<E> temp =Head;  while(Temp.next! =NULL) {Temp=Temp.next; Result+ = Temp.data + ","; }        returnresult; }    //Establish a Inner Node class//Leaf Type    Private classNode<e>{        PrivateE data; PrivateNode<e>Next;  PublicNode () { This. data =NULL;  This. Next =NULL; }                 PublicNode (E data, node<e>next) {             This. data =data;  This. Next =Next; }    }    }

Implementation of java-single-link list

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.