java中链表结点的创建

package test;
//目标:创建链表
//结点类代码块
class Node{
private String data;//结点数据
private Node node;//构建结点对象
public Node(String data) {
this.data=data;
}//设置下一个结点
public void setNodeNext(Node node){
this.node=node;
}//返回结点内数据
public String getData(){
return this.data;
}
public Node getNodeNext(){
return this.node;
}

}
public class T_4 {

public static void main(String[] args) {
	// TODO Auto-generated method stub//实现
		Node node=new Node("head");//创建结点并实例化
		Node n1=new Node("1");
		Node n2=new Node("2");
		Node n3=new Node("3");
		node.setNodeNext(n1);n1.setNodeNext(n2);n2.setNodeNext(n3);//链接
		Node now=node;
		while(now!=null){
		System.out.println(now.getData());	
		now=now.getNodeNext();//下一个结点的切换
		}
		
}

}

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

猜你喜欢

转载自blog.csdn.net/qq_45393395/article/details/97616159