Java implementation single-linked list

Source: Internet
Author: User

Early in the morning, the first thing is to open the blog Park casually look at the blog, see an article mentioned in the algorithm data structure, to achieve a single-linked list operation. The author is written in C #, so I'm going to get it through Java.

First define a node class

 Public classNode {protectedNode Next;//pointer Field      Public  intData//data fields             PublicNode (intdata) {              This. data =data; }              //Show this node      Public voiddisplay () {System. Out.print (Data+ " "); }  }

Next, define a single-linked list and implement the relevant methods:

 Public classlinklist { PublicNode first;//define a head node.    Private intpos = 0;//the location of the node     Publiclinklist () { This. First =NULL; }    //Insert a head node     Public voidAddfirstnode (intdata) {Node Node=NewNode (data); Node.next=First ; First=node; }    //deletes a head node and returns the head node .     Publicnode Deletefirstnode () {node Tempnode=First ; First=Tempnode.next; returnTempnode; }    //Insert a node at any point after index     Public voidAddintIndexintdata) {Node Node=NewNode (data); Node Current=First ; Node previous=First ;  while(POS! =index) {Previous=Current ; Current=Current.next; POS++; } Node.next=Current ; Previous.next=node; POS= 0; }    //remove nodes from any location     PublicNode Deletebypos (intindex) {Node current=First ; Node previous=First ;  while(POS! =index) {POS++; Previous=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; } Else{pos= 0; Previous.next=Current.next; }        returnCurrent ; }    //Delete nodes based on the data of the node (delete only the first one)     PublicNode Deletebydata (intdata) {Node current=First ; Node previous= First;//Remember the previous node         while(Current.data! =data) {            if(Current.next = =NULL) {                return NULL; } Previous=Current ; Current=Current.next; }        if(Current = =First ) { First=First.next; } Else{Previous.next=Current.next; }        returnCurrent ; }    //Show all the node information     Public voiddisplayallnodes () {Node current=First ;  while(Current! =NULL) {current.display (); Current=Current.next;    } System.out.println (); }    //finding node information based on location     PublicNode Findbypos (intindex) {Node current=First ; if(POS! =index) { Current=Current.next; POS++; }        returnCurrent ; }    //finding node information based on data     PublicNode FindByData (intdata) {Node current=First ;  while(Current.data! =data) {            if(Current.next = =NULL)                return NULL; Current=Current.next; }        returnCurrent ; }}

Finally, we can test the test class to do the relevant tests:

 Public classTestlinklist { Public Static voidMain (string[] args) {linklist linklist=Newlinklist (); Linklist.addfirstnode (20); Linklist.addfirstnode (21st); Linklist.addfirstnode (19); //print19,21,20Linklist.add (1, 22);//print19,22,21,20Linklist.add (2, 23);//print19,22,23,21,20Linklist.add (3, 99);//print19,22,23,99,21,20//calling this method will print 19,22,23,99,21,20linklist.displayallnodes (); }}

At this point, the single-linked list of operations on the note here.

Java implementation single-linked 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.