zoukankan      html  css  js  c++  java
  • 单向链表的元素查找和删除

    整个过程以根节点为基础,先确定根节点的情况,再一次类推

     1 package test02;
     2 
     3 /*
     4  * 单向链表的正宗实现
     5  * */
     6 
     7 class Link{
     8     class Node{
     9         private String data;
    10         private Node next;
    11         public Node(String data){
    12             this.data = data;
    13         }
    14         public void addNode(Node newNode){
    15             if(this.next == null){
    16                 this.next = newNode;
    17             }else{
    18                 this.next.addNode(newNode);
    19             }
    20         }
    21         public void printNode(){
    22             System.out.println(this.data);
    23             if(this.next != null){
    24                 this.next.printNode();
    25             }
    26         }
    27         public boolean searchNode(String data){        //内部定义搜索方法
    28             if((this.data).equals(data)){            //判断当前节点内容是否与查找的一致
    29                 return true;
    30             }else{
    31                 if(this.next != null){
    32                     return this.next.searchNode(data);
    33                 }else{
    34                     return false;
    35                 }
    36             }
    37         }
    38         public void deleteNode(String data){        //删除节点,仍然以根节点为基础
    39             if(this.next == null){
    40                 
    41             }else{
    42                 if((this.next.data).equals(data)){
    43                     this.next = this.next.next;
    44                 }else{
    45                     this.next.deleteNode(data);
    46                 }
    47             }
    48         }
    49     }
    50     Node root;
    51     boolean b;
    52     public void add(String data){
    53         Node newNode = new Node(data);        //第一步就是生成节点,接下来就可以参考链表的简单实现方法
    54         if(this.root == null){                //抓住根节点是首要任务,以后的其他操作都可以建立在根节点上
    55             this.root = newNode;
    56         }else{
    57             this.root.addNode(newNode);
    58         }
    59     }
    60     public void printnode(){
    61         this.root.printNode();
    62     }
    63     public boolean contains(String data){              //判断元素是否存在    
    64         return this.root.searchNode(data);    
    65         
    66     }
    67     public void delete(String data){
    68         if(this.root == null){
    69             
    70         }else{
    71             if((this.root.data).equals(data)){
    72                 this.root = this.root.next;
    73                 this.delete(data);                    //如果根节点的下一个节点也是查找内容,则递归
    74             }else{
    75                 this.root.deleteNode(data);
    76             }
    77         }
    78     }
    79 }
    80 
    81 public class LianBiao01 {
    82 
    83     public static void main(String[] args) {
    84         Link l = new Link();
    85         l.add("ROOT");
    86         l.add("A");
    87         l.add("B");
    88         l.add("C");
    89         l.printnode();
    90         System.out.println(l.contains("B"));
    91         System.out.println(".....");
    92         l.delete("B");
    93         
    94         l.printnode();
    95         
    96     }
    97 }
  • 相关阅读:
    用 tableExcel导出EXCEL数据
    个人作业——软件评测
    结对第二次作业——某次疫情统计可视化的实现
    软工实践寒假作业(1/2)
    java注解和反射
    共享密钥
    鲁棒性验证-第五小组
    维数约减报告--第五小组
    网络1911、1912 D&S第1次作业--线性表批改总结
    网络1911、1912 C语言第4次作业--函数批改总结
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7417246.html
Copyright © 2011-2022 走看看