java 单向链表实现

 1 class Node{//Node类
2 private String data;
3 private Node next;
4 public Node(String data){
5 this.data=data;
6 }
7 public String getData(){
8 return this.data;
9 }
10 public void setNext(Node next){
11 this.next=next;
12 }
13 public Node getNext(){
14 return this.next;
15 }
16 }
17
18 public class LinkDemo1 {//主函数
19 public static void main(String args[]){
20 Node root=new Node("火车头");
21 Node n1=new Node("车厢A");
22 Node n2=new Node("车厢B");
23 Node n3=new Node("车厢C");
24 root.setNext(n1);
25 n1.setNext(n2);
26 n2.setNext(n3);
27 printNode(root);
28
29 }
30 public static void printNode(Node node){//递归输出链表
31 System.out.print(node.getData()+"\t");
32 if(node.getNext()!=null)
33 printNode(node.getNext());
34
35 }
36
37 } 判断一个结点以后是否还有后续结点,如果有,则输出,无,则采用递归的方法输出。。。

转载于:https://www.cnblogs.com/dennisac/articles/light_sky.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值