5. 面对对象(基础) Part 4 --- 学习笔记

来源:互联网 发布:算法导论公开课 网盘 编辑:程序博客网 时间:2024/04/29 14:58

5.15 实例讲解
     1. 单向链表的实现(1)
        单项链表表示如图:
           
class PriNode{
     private String data;
     private PriNode next;

     //Constructor
     public PriNode(String data){
          this.data = data;
     }
     public String getData(){
          return this.data;
     }
     public void  setNext(PriNode next){
          this.next = next;
     }
     public PriNode getNext(){
          return this.next;
     }
}

public class TestPriNode(){
     public static void main(String args[]){
          // set a new link list
          PriNode root = new PriNode("Train Head");
          PriNode node1 = new PriNode("Train One");
          PriNode node2 = new PriNode("Train Two");
          PriNode node3 = new PriNode("Train Three");
          PriNode node4 = new PriNode("Train Four");
          root.setNext(node1);
          node1.setNext(node2);
          node2.setNext(node3);
          node3.setNext(node4);

          //print the link out
          printNode(root);    
     }

     public static void printNode(PriNode node){
          System.out.print(node.getData + "\t\t");
          if (node.getNext() != null){
               printNode(node.getNext());
          }
     }
}

     2. 单向链表的实现(2)
        链表的增加:直接增加到链表的结尾处。
        链表的删除:需作如下图所示的处理
               
注意到,这里用到了内部类来处理node.
 *****打印节点都是从根节点开始打印的。所以这里的代码的切入点都是从根节点!  我要的就是这种思想!!!! *****
需要注意的是编写代码的顺序,个人见解,代码的编写顺序也可以增强逻辑性,so, 以增加节点为例
1. 增加节点:   第一步编写颜色部分,第二步颜色部分,第三步颜色部分,第四步颜色部分,第五步颜色部分
2. 增加节点后,接着就是打印节点来验证是否正确,所以就需要新增打印节点方法printNode.  第六步颜色部分
3. 有了链表后,进行删除操作(第七步颜色部分),查找操作。
class PriLink{
        class PriNode(){
                private String data;
                private PriNode next;
                public PriNode(String data){
                        this.data = data;
                }

                public void add(PriNode newNode){
                        if (this.next == null){
                                this.next = newNode;
                        }else {
                                this.next.add(newNode);
                        }
                }

                //*****print node part III*****
                public void print(){
                        System.out.print(this.data +"\t\t");
                        if (this.next != null){
                                this.next.print();
                        }
                }

                //***** delete node part IV*****
                public boolean search(String data){  //NOTE: Here contains 字符串的比较方法!!!
                        if (data.equals(this.data)){
                                return true;
                        }else {
                                if(this.next !=null){
                                        return this.next.search(data);
                                }else{
                                        return false;
                                }
                }
        
                //*****delete node part VI *****
                public void delete(PriNode previous, String data){
                        if (data.equals(this.data)){                                  //找到了匹配的节点
                                previous.next = this.next;                           //空出当前节点
                        } else {
                                if(this.next != null){
                                        this.next.delete(this, data);                 //继续向下找
                                }
                        }
                }

        }

        private PriNode root;

        public addNode(String data){
                PriNode newNode = new PriNode(data);
                if (this.root == null){
                        this.root = newNode;
                }else {
                        this.root.add(newNode)
                }

        }

        //*****delete node part III*****
        public boolean contains(String name){              //判断元素是否存在
                return this.root.search(name);                  //调用PriNode类中的search方法。
        }


        // ***** delete node part II*****
        public deleteNode(String data){
                if (this.contains(data)){                                 //如果节点存在,则执行操作
                        // *****delete node part V*****
                        if (this.root.data.equals(data)){            //判断根节点是否满足条件
                                this.root = this.root.next;               //将根节点之后的那个节点设置为新的根节点
                        }else{
                                this.root.next.delete(root, data);       //删除节点
                        }

                }
        }


        //*****print node part II*****
        public printNode(){
                if (this.root != null){
                        this.root.print();
                }
        }

}

public class LinkTest{
        public static void main(String args[]){
                PriLink pl = new PriLink();
                pl.addNode("A");                                           //add node A,B,C,D,E
                pl.addNode("B");
                pl.addNode("C");
                pl.addNode("D");
                pl.addNode("E");

                //*****print node part I*****
                pl.printNode();                               //print the link out to check that if add function works fine or not.

                // *****delete node part I*****
                pl.deleteNode("D");                                       //delete one node that exists in the link list
                pl.printNode();
                pl.deleteNode("c");                                        //try to delete node that does not exists in the link list.
                pl.printNode();
                

        }
}

课后思考:
上面的实现二中,有如下问题:
     删除操作,先判断元素是否存在时,比较了链表的data是否相符,但是在真正执行删除操作调用delete方法时,里面又比较了链表的data! 这与search里面比较data算是重复操作么??可以在search里面比较data的时候直接返回PriNode对象么?这样性能是不是更好点呢???!!!  我是个菜鸟初学者,还解决不了啊,先mark下,到后面看能不能发飚解决~~


0 0
原创粉丝点击