zoukankan      html  css  js  c++  java
  • 简单的单向链表实例代码-java版

    《java开发实战》里的代码

    单向链表最简单的实现:

     1 class Node //定义节点类
     2 {
     3     private String data;
     4     private Node next;
     5     //构造方法
     6     public Node(String data){
     7         this.data=data;
     8     }
     9 
    10     //设置下一节点
    11     public void setNext(Node next){
    12         this.next=next;
    13     }
    14     //获取下一节点
    15     public Node getNext(){
    16         return this.next;
    17     }
    18 
    19     //获取节点信息
    20     public String getData(){
    21         return this.data;
    22     }
    23 }
    24 
    25 public class LinkDemo1
    26 {
    27     public static void main(String args[]){
    28         //实例以下节点
    29         Node root=new Node("A");
    30         Node node1=new Node("B");
    31         Node node2=new Node("C");
    32         Node node3=new Node("D");
    33         //建立节点之间的关系 root->node1->node2->node3
    34         root.setNext(node1);
    35         node1.setNext(node2);
    36         node2.setNext(node3);
    37         
    38         //输出所有节点
    39         printNode(root);
    40 
    41 
    42     }
    43     //定义输出所有节点的方法
    44     public static void printNode(Node node){
    45         //输出节点信息
    46         System.out.print(node.getData()+"-->");
    47 
    48         //如果还有节点
    49         if(node.getNext()!=null){
    50             printNode(node.getNext());//再调用方法本身
    51         }else{
    52             System.out.print("null");
    53         }
    54     }
    55 }

    运行结果:

    经过封装后的单向链表的实现:

      1 class Link //链表类
      2 {
      3     class Node//节点类
      4     {
      5         private String data;
      6         private Node next;
      7         //构造方法
      8         public Node(String data){
      9             this.data=data;
     10         }
     11         
     12         //添加节点
     13         public void add(Node next){
     14             //当前节点如果为空
     15             if(this.next==null){
     16                 this.next=next;//将参数节点赋给当前节点
     17             }else{
     18                 this.next.add(next);//如果当前节点不为空,则递归调用添加方法直到有空节点为止
     19             }
     20         }
     21 
     22         //输出所有节点
     23         public void print(){
     24             System.out.print(this.data+"	");//输出当前节点
     25             if(this.next!=null){//如果还有下一节点,则递归调用输出方法,直到下一节点为空。
     26                 this.next.print();
     27             }
     28         }
     29 
     30         //查找节点
     31         public boolean search(String data){
     32             if(data.equals(this.data)){//比较字符串用equals,=号是比较对象
     33                 return true;
     34             }else{
     35                 if(this.next!=null){//如果还有下一节点,则还是递归调用方法本身。
     36                     return this.next.search(data);
     37                 }else{
     38                     return false;
     39                 }
     40             }
     41         }
     42 
     43         //删除节点(实质是变换根节点对下一节点的引用)
     44         public void delete(Node previous,String data){
     45             if(data.equals(this.data)){//这里还是用到查找的判断,
     46                 previous.next=this.next;
     47             }else{
     48                 if(this.next!=null){
     49                     this.next.delete(this,data);
     50                 }
     51             }
     52         }
     53     }
     54 
     55     private Node root;//定义根节点。
     56 
     57     //进一步封装添加节点的方法
     58     public void addNode(String data){
     59         Node newNode=new Node(data);//实例一个节点
     60 
     61         if(this.root==null){
     62             this.root=newNode;//如果根节点为空,则将上面的实例给它
     63         }else{
     64             this.root.add(newNode);//否则添加在根节点的下一节点里。
     65         }
     66     }
     67 
     68     //进一步封装输出所有节点方法
     69     public void printNode(){
     70         if(this.root!=null){//这里没什么可说的,根节点不为空才有输出的意义。
     71             this.root.print();
     72         }
     73     }
     74     
     75     //进一步封装查找节点的方法
     76     public boolean contains(String name){
     77         return this.root.search(name);//查询调用节点类的查找方法
     78     }
     79 
     80     //进一步封装删除节点的方法
     81     public void deleteNode(String data){
     82         if(this.contains(data)){//用到查找节点的判断,如果要删除的节点存在
     83             if(this.root.data.equals(data)){
     84                 this.root=this.root.next;//变换节点引用,将当前节点更换为下一节点
     85             }else{
     86                 this.root.next.delete(root,data);//递归遍历所有节点
     87             }
     88         }
     89     }
     90 }
     91 
     92 public class LinkDemo2
     93 {
     94     public static void main(String[]args){
     95         Link link=new Link();
     96         link.addNode("A");
     97         link.addNode("B");
     98         link.addNode("C");
     99         link.addNode("D");
    100 
    101 
    102         System.out.println("
    ==========删除之前===========");
    103         link.printNode();//输出所有节点。
    104 
    105         link.deleteNode("C");//删除C节点
    106 
    107         System.out.println("
    ==========删除之后===========");
    108         link.printNode();//再次输出所有节点
    109 
    110         System.out.println("
    ");
    111     
    112         //查找指定的节点
    113         System.out.println("要查找的节点C是否存在:"+link.contains("C"));
    114     }
    115 }

    运行结果:

  • 相关阅读:
    IO流2 --- File类的常用方法1 --- 技术搬运工(尚硅谷)
    IO流1 --- File类的实例化 --- 技术搬运工(尚硅谷)
    luoguP6136 【模板】普通平衡树(数据加强版)
    CF981E Addition on Segments 线段树分治+bitset
    LOJ#2538. 「PKUWC2018」Slay the Spire DP+组合
    LOJ#2537. 「PKUWC2018」Minimax 线段树合并
    luoguP4220 [WC2018]通道 随机化
    学习笔记2018/6/22
    git push解决办法: ! [remote rejected] master -> master (pre-receive hook declined)
    IDEA错误:Cannot start compilation: the output path is not specified for module "Test". Specify the out
  • 原文地址:https://www.cnblogs.com/comrd/p/3575817.html
Copyright © 2011-2022 走看看