zoukankan      html  css  js  c++  java
  • 单向链表的Java实现

    1、

     1 class 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 public class LinkDemo01 {
    18 
    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     private static void PrintNode(Node node) {
    31         System.out.println(node.getData() + "	");
    32         if(node.getNext() != null){
    33             PrintNode(node.getNext());
    34         }
    35     }

    输出结果为:

    火车头
    车厢A
    车厢B
    车厢C

    2、

     1 class Link{
     2     class Node{
     3         private String data;
     4         private Node next;
     5         public Node(String data){
     6             this.data = data;
     7         }
     8         public void add(Node newNode){
     9             if(this.next == null){
    10                 this.next = newNode;
    11             }else{
    12                 this.next.add(newNode);
    13             }
    14         }
    15         public void print(){
    16             System.out.println(this.data + "	");
    17             if(this.next != null){
    18                 this.next.print();
    19             }
    20         }
    21         public boolean search(String data){
    22             if(data.equals(this.data)){
    23                 return true;
    24             }else{
    25                 if(this.next != null){
    26                     return this.next.search(data);
    27                 }else{
    28                     return false;
    29                 }
    30             }
    31         }
    32         public void delete(Node previous,String data){
    33             if(data.equals(this.data)){
    34                 previous.next = this.next;
    35             }else{
    36                 if(this.next != null){
    37                     this.next.delete(this, data);
    38                 }
    39             }
    40         }
    41     }
    42     private Node root;
    43     public void addNode(String data){
    44         Node newNode = new Node(data);
    45         if(this.root == null){
    46             this.root = newNode;
    47         }else{
    48             this.root.add(newNode);
    49         }
    50     }
    51     public void printNode(){
    52         if(this.root != null){
    53             this.root.print();
    54         }
    55     }
    56     public boolean contain(String name){
    57         return this.root.search(name);
    58     }
    59     public void deletNode(String data){
    60         if(this.contain(data)){
    61             if(this.root.data.equals(data)){
    62                 this.root = this.root.next;
    63             }else{
    64                 this.root.next.delete(root, data);
    65             }
    66         }
    67     }
    68 }
    69 
    70 public class LingDemo02 {
    71 
    72     public static void main(String[] args) {
    73         Link l = new Link();
    74         l.addNode("A");
    75         l.addNode("B");
    76         l.addNode("C");
    77         l.addNode("D");
    78         l.addNode("E");
    79         
    80         l.printNode();
    81         
    82         l.deletNode("C");
    83         
    84         System.out.println();
    85         
    86         l.printNode();
    87         
    88         System.out.println();
    89         
    90         System.out.println(l.contain("A"));
    91         
    92     }
    93 
    94 }

    输出结果为:

    A
    B
    C
    D
    E

    A
    B
    D
    E

    true

  • 相关阅读:
    关于Maven项目build时出现No compiler is provided in this environment的处理
    freemaker的函数使用
    FTP在docker容器中上传失败解决,改为被动模式
    linux重定向及nohup不输出的方法
    手动抠下的wordpress登录页面样式
    使用后端生成图片验证码流文件(不推荐)
    部署到docker容器后图片验证码显示不出来
    Linux修改profile文件改错了,恢复的方法
    DotNETCore 学习笔记 异常处理
    DotNETCore 学习笔记 路由
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7326016.html
Copyright © 2011-2022 走看看