码迷,mamicode.com
首页 > 其他好文 > 详细

单向链表的元素查找和删除

时间:2017-08-23 13:40:18      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:oid   false   链表   nbsp   return   private   div   boolean   log   

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

 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 }

 

单向链表的元素查找和删除

标签:oid   false   链表   nbsp   return   private   div   boolean   log   

原文地址:http://www.cnblogs.com/XuGuobao/p/7417246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!