Create a linked list with Java __java

Source: Internet
Author: User
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");
 }

}


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.