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