Preface:
Java is not like C, C + + can establish a pointer to form a linked list, create a structure and then connect to the chain. Java is an object-oriented programming, so his basic unit is class. train of thought: It is still building the class, where the inner class is used. Create a class as link, where an internal class is created. Link can use the Add method to add a node and print it by using print.
Package ZX;
Class link{private Node root;
Class node{private String name;
Private Node Next;
Public Node (String name) {this.name = name;
Public String GetName () {return this.name; The public void AddNode (Node newNode) {if (this. Next==null) {this.
Next = NewNode; }else{this.
Next.addnode (NewNode);
} public void Printnode () {System.out.print (this.name + "-->"); if (this. Next!=null) {this.
Next.printnode ();
}
}
};
public void Add (String name) {Node NewNode = new node (name);
if (this.root==null) {this.root = NewNode;
}else{This.root.addNode (NewNode);
}} public void print () {if (this.root!=null) {this.root.printNode ();
}
}
}; public class Linkdemo {/** * @param args */public static void main (string[] args) {//TODO auto-generated Meth
od stub link = new link ();
Link.add ("root node");
Link.add ("First node"); Link.add ("Second Node ");
Link.add ("Third node");
Link.add ("The Forth Node");
Link.print ();
SYSTEM.OUT.PRINTLN ("null");
}
}