Refers to the linked list can be used to achieve increased data, modify, delete, query operation
The basic structure of the program
Prior to the development of specific list available, you must first clear a truth: Node class is responsible for storing and matching node relationship of all node data, it is impossible to separate the Node class to use. And the implementation of a class which is Node can use a separate, external bypass can be used directly Node Link category class, this is obviously the absence of any sense. Therefore, the following must modify the design structure, so that the Node class is used only category Link
This is evident when using a suitable internal class choice. Internal class can define private use, so that the internal class can be used by an external class, another point, the internal class can be a private property between the external and the class to facilitate a direct access
Linked list structure development
class Link { // list class, a class which can only see the outside // defined internally, such services mainly Link Private class the Node { // class definition of Private String Data; // save data Private the Node Next; // reference relationship public the Node (Data String) { the this .data = Data; } // =================== above internal class ========================= === } Private the Node the root; // root }
And then mainly be perfect filling code and function
Increased data
public void add (variable data type)
If you want to add new data, you should node object class is responsible for an increase in the Link, Link and maintained by the root class, all the nodes matching relation to the Node handles
class Link { // list class, a class which can only see the outside // defined internally, such services mainly Link Private class the Node { // class definition of Private String Data; // save data Private the Node Next; // reference relationship public the Node (Data String) { the this .data = Data; } public void the AddNode (the Node the newNode) { IF ( the this .next == null ) { // this next node is empty the this .next = the newNode; } The else { // rearwardly continue saving the this .next.AddNode (the newNode); } } public void printNode(){ // 打印Node信息 System.out.println(this.data); if( this.next != null){ this.next.printNode(); } } // =================== above internal class ========================= === } Private the Node the root; // root public void the Add (String Data) { // assumption does not allow null IF (Data == null ) { return ; } The newNode the Node = new new the Node (Data); // data to store IF ( the this .Root == null ) { // if the current is not the root node, the root node is set to the this .Root = the newNode; // save root } the else { // the presence of the root node, the stored data to the next node to find the this .root.AddNode (the newNode); } } public void Print () { // print all Node Information root.printNode (); } } public class LinkDemo{ public static void main(String args[]){ Link all = new Link(); all.add("Hello"); all.add("World"); all.add(null); all.print(); } }
In this case the use of a null allowed for the determination. But not all lists are not allowed to be null