java中链表的创建和遍历

输出结果
package test;
//目标:实现链表结点的遍历
class Node_6{
private String data;
private Node_6 nextNode;
public Node_6(String indata){
this.data=indata;
}
public void setNextNode(Node_6 node_6){
this.nextNode=node_6;
}
public String getData(){
return this.data;
}
public Node_6 getNextNode(){
return this.nextNode;
}
public void print(Node_6 nodeHead){
if(nodeHead.getNextNode()!=null){
System.out.println(nodeHead.getData());
nodeHead=nodeHead.getNextNode();
print(nodeHead);
}
else{
System.out.println(nodeHead.getData());
}
}

}
public class T_6 {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	Node_6 head=new Node_6("head");
	Node_6 j1=new Node_6("1");
	Node_6 j2=new Node_6("2");
	Node_6 j3=new Node_6("3");
	Node_6 j4=new Node_6("4");
	head.setNextNode(j1);
	j1.setNextNode(j2);
	j2.setNextNode(j3);
	j3.setNextNode(j4);
	head.print(head);
}

}

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

猜你喜欢

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