现在的位置: 首页 > 综合 > 正文

java 单向链表实现

2018年09月14日 ⁄ 综合 ⁄ 共 665字 ⁄ 字号 评论关闭
 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 } 判断一个结点以后是否还有后续结点,如果有,则输出,无,则采用递归的方法输出。。。

抱歉!评论已关闭.