刚接触的链表简单程序

Code:
  1. class Link{   
  2.     class Node{   
  3.         private String name ;   // 保存节点的名字   
  4.         private Node next ;     // 保存下一个节点   
  5.         public Node(String name){   
  6.             this.name = name ;   
  7.         }   
  8.         public String getName(){   
  9.             return this.name ;   
  10.         }   
  11.         public void addNode(Node newNode){   
  12.             if(this.next==null){    // 后面没有东西   
  13.                 this.next = newNode ;   
  14.             }else{   
  15.                 this.next.addNode(newNode) ;    // 向下继续查   
  16.             }   
  17.         }   
  18.         public void printNode(){   
  19.             System.out.print(this.name + " --> " ) ;   
  20.             if(this.next!=null){   
  21.                 this.next.printNode() ; // 向下继续列出   
  22.             }   
  23.         }   
  24.         public boolean searchNode(String name){   
  25.             if(this.name.equals(name)){   
  26.                 return true ;   
  27.             }else{   
  28.                 if(this.next!=null){   
  29.                     return this.next.searchNode(name) ;   
  30.                 }else{   
  31.                     return false ;   
  32.                 }   
  33.             }   
  34.         }   
  35.         public void deleteNode(Node preNode,String name){   
  36.             if(this.name.equals(name)){   
  37.                 preNode.next = this.next ;   
  38.             }else{   
  39.                 this.next.deleteNode(this,name) ;   
  40.             }   
  41.         }   
  42.     };   
  43.     private Node root ; // 要定义出根节点   
  44.     public void add(String name){   
  45.         Node newNode = new Node(name) ;   
  46.         if(this.root==null){    // 没有根节点,则把第一个作为根节点   
  47.             this.root = newNode ;   
  48.         }else{   
  49.             this.root.addNode(newNode) ;   
  50.         }   
  51.     }   
  52.     public void print(){   
  53.         if(this.root!=null){   
  54.             this.root.printNode() ;   
  55.         }   
  56.     }   
  57.     public boolean search(String name){ // 指定查找的名字   
  58.         if(this.root!=null){   
  59.             return this.root.searchNode(name) ;   
  60.         }else{   
  61.             return false ;   
  62.         }   
  63.     }   
  64.     public void delete(String name){   
  65.         if(this.search(name)){  // 判断此节点是否存在   
  66.             if(this.root.name.equals(name)){   
  67.                 if(this.root.next!=null){   
  68.                     this.root = this.root.next ;    // 改变根节点   
  69.                 }else{   
  70.                     this.root = null ;  // 取消   
  71.                 }   
  72.             }else{   
  73.                 if(this.root.next!=null){   
  74.                     this.root.next.deleteNode(root,name) ;   
  75.                 }   
  76.             }   
  77.         }   
  78.     }   
  79. };   
  80. public class LinkDemo03{   
  81.     public static void main(String args[]){   
  82.         Link link = new Link() ;   
  83.         link.add("根节点") ;   
  84.         link.add("第一节点") ;   
  85.         link.add("第二节点") ;   
  86.         link.add("第三节点") ;   
  87.         link.add("第四节点") ;   
  88.         link.add("第五节点") ;   
  89.         link.print() ;   
  90.         System.out.println() ;   
  91.         // System.out.println(link.search("第x节点")) ;   
  92.         link.delete("第四节点") ;   
  93.     //  link.delete("根节点") ;   
  94.         link.print() ;   
  95.     }   
  96. };  

虽然现在看的还是模模糊糊,程序本身涉及的内容有内部类和this关键字,好好参悟....O(∩_∩)O~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值