/*** 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