zoukankan      html  css  js  c++  java
  • 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()+"	");
    32 if(node.getNext()!=null)
    33 printNode(node.getNext());
    34  
    35 }
    36 
    37 }
    判断一个结点以后是否还有后续结点,如果有,则输出,无,则采用递归的方法输出。。。
  • 相关阅读:
    slf4j+log4j2的配置
    日志规约
    log4j2配置文件log4j2.xml详解(转载)
    好用的打包工具webpack
    gulp插件
    学习自动化工具gulp
    git
    nodejs学习随笔
    好用的meta标签
    小问题记录
  • 原文地址:https://www.cnblogs.com/zsqfightyourway/p/7115629.html
Copyright © 2011-2022 走看看