您的位置:首页 > 编程语言 > Java开发

java 单向链表实现

2013-07-12 11:23 323 查看
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 }
判断一个结点以后是否还有后续结点,如果有,则输出,无,则采用递归的方法输出。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: