Alas, say a little nonsense, yesterday occasionally saw myself a year ago with the C solution to Joseph Ring problem of the circular chain list, sigh unceasingly, think oneself a year ago embedded dream, these two days happened a lot, and some people are not really in, the mood is not good, not much said, directly on the code, just some of the basic operation of the list, take some kung fu to look good.
First, set up a node class, inside a node object and data (to distinguish);
Public classNode {protectedNode Next;//Pointers protected intData//Data PublicNode (intdata) { This. data =data; } //Display Node Public voiddisplay () {System.out.println (data+" "); }}
Then create a linklist class that contains some basic methods of the linked list,
Public classlinklist { PublicNode first;//define a head 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 nodes. Publicnode Deletefirstnode () {node Tempnode=First ; First=Tempnode.next; returnFirst ; } //Replace the node that is behind index. Public voidAddintIndex,intdata) {Node Node=First ; Node Current=First ; while(index-->0) { current=Node.next; Node=Current ; } current.data=data; } //Insert a node after the index node Public voidInsert (intIndex,intdata) {Node Node=NewNode (data); Node Current=First ; Node privious=First ; while(index-->0) {privious=Current ; Current=Current.next; } Node.next=Current ; Privious.next=node; } //remove nodes from any location Public voidDeleteintindex) {Node current=First ; Node privious=First ; while(index-->0) {privious=Current ; Current=Current.next; } if(Current = =First ) { First=First.next; }Else{Privious.next=Current.next; } } //deletes the node based on the value of data, deleting the first node found Public voidDeleteData (intdata) {Node privious=First ; Node Current=First ; while(current.data!=data) {privious=Current ; Current=Current.next; } if(Current = =First ) { First=First.next; }Else{Privious.next=Current.next; } }}
And then create a main class,
Public classTestlink { Public Static voidMain (string[] args) {linklist L=Newlinklist (); L.addfirstnode (1); L.addfirstnode (2); L.addfirstnode (3); Node Node=L.first; L.insert (2, 5); L.deletedata (5); while(node!=NULL) {node.display (); Node=Node.next; } }}
is not in the mood to continue to write, hope will be better in the future, August 9, 2015 18:55 minutes.
The list structure of Java programming