LinkList01

来源:互联网 发布:方圆设计软件下载 编辑:程序博客网 时间:2024/06/01 20:15

实例讲解2-单项链表一

class Node

{

  privateString data;

  privateNode next;

  

  publicNode(String data){ this.data = data; }

  

  publicString getData(){ return data; }

  publicNode getNext(){ return next; }

  

  publicvoid setNext(Node next){ this.next = next; }

}

 

public class LinkDemo1

{

  publicstatic void main(String args[] )

  {

      NodeL = new Node("火车头");

      Noded1 = new Node("车厢-A");

      Noded2 = new Node("车厢-B");

      Noded3 = new Node("车厢-C");

      

      L.setNext(d1);

      d1.setNext(d2);

      d2.setNext(d3);

      

      printLink(L);

  }

  publicstatic void printLink(Node node)

  {

      System.out.print(node.getData());

      

      if(node.getNext()!= null)

      {

         System.out.print("-->");

         printLink(node.getNext());

      }

  }

}

class Node{    private String data;    private Node next;        public String getData(){        return this.data;    }    public void setNext(Node next){        this.next = next;    }    public Node getNext(){        return this.next;    }    public Node(String data){        this.data = data;    }}public class LinkListDemo01{    public static void main(String args[]){        Node root = new Node("train");        Node node_1 = new Node("A");        Node node_2 = new Node("B");        Node node_3 = new NOde("C");        root.setNext(node_1);        node_1.setNext(node_2);        rode_2.setNext(node_3);        printNode(root);    }        public static void printNode(Node node){        System.out.print(node.getData);        if(node.getNext()!=null){            System.out.print("→");            printNode(node.getNext());        }    }}

说明:此文章仅为作者学习时方便移动端随时查阅。不对任何读者负责!