java中内部类创建结点并遍历

package test;
//目标:使用内部类创建结点并遍历
class LinkNode{
private class Node{
private String data;
private Node next;
public Node(String data){
this.data=data;
}
public String getData(){
return this.data;
}
public Node getNextNode(){
return this.next;
}
public void addNode(Node newNode){
if(this.nextnull){
this.next=newNode;
}
else{
this.next.addNode(newNode);
}
}
}
private Node root;
public void addNode(String data){
if(data
null) return;
Node newNode=new Node(data);
if(this.root==null){
this.root=newNode;
}
else{
this.root.addNode(newNode);
}
}
public void print(){
Node node=root;
while(node!=null){

		System.out.println(node.getData());
		node=node.getNextNode();
		
	}
}

}
public class T_7 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	LinkNode linkNode=new LinkNode();
	linkNode.addNode("head");
	linkNode.addNode("1111");
	linkNode.addNode("2222");
	linkNode.addNode("1111");
	linkNode.addNode("2222");
	linkNode.print();

}

}

发布了18 篇原创文章 · 获赞 3 · 访问量 1053

猜你喜欢

转载自blog.csdn.net/qq_45393395/article/details/97669613
今日推荐