zoukankan      html  css  js  c++  java
  • 可复用的链表

    采用面向对象的方法设计一个线性表,目的是为其他需要用线性表的应用提供线性表对象。

    思路:写一个线性表类,在类中实现Node的定义,来定义每一个点,同时实现増删改查的操作。

    class Link{
        class Node{
            private String data;
            private Node next;
            public Node(String data){
                this.data=data;
            }
            public void add(Node newNode){//节点中的增加方法
                if(this.next==null){
                    this.next=newNode;
                }
                else{
                    this.next.add(newNode);
                }
            }
            public void print(){
                System.out.print(this.data+"	");
                if(this.next!=null)
                    this.next.print();
            }
            public boolean search(String data){
                if(data.equals(this.data)){
                    return true;
                }
                else{
                    if(this.next!=null)
                        return this.next.search(data);
                    else
                        return false;
                }
            }
            public void delete(Node pre,String data){
                if(data.equals(this.data))
                    pre.next=this.next;
                else{
                    if(this.next!=null){
                        this.next.delete(this,data);
                    }
                }
            }
        };
        private Node root;
        public void addNode(String data){
            Node newNode=new Node(data);
            if(this.root==null) 
                this.root=newNode;
            else
                this.root.add(newNode);
        }
        public void printNode(){
            if(this.root!=null){
                this.root.print();
            }
        }
        public boolean findNode(String name){
            return this.root.search(name);
        }
        public void deleteNode(String data){
            if(this.findNode(data)){
                if(this.root.data.equals(data)){
                    this.root=this.root.next;
                }
                else
                    this.root.next.delete(root,data);
            }
            else
                System.out.println("要删除的节点不存在");
        }
    };
    public class link01{
        public static void main(String args[]){
            Link H=new Link();
            H.addNode("A");
            H.addNode("B");
            H.addNode("C");
            H.addNode("D");
            H.addNode("E");
            H.addNode("F");
            H.printNode();
            System.out.println();
            H.deleteNode("A");
            H.deleteNode("D");
            H.printNode();
            System.out.println();
            System.out.println(H.findNode("E"));
            H.deleteNode("X");
    
        }
    }

     但是,这样是不够的。按照题目的要求,这个线性表不是一个简单的线性表,看要求“为其他需要线性表的应用提供线性表对象”,这个要求体现了线性表的可复用性。因此将上述Node类中的String data改造一下,改为Object定义的数据,因为Object是所有类的父类,因此可以用其他任何类来实现初始化,并且用到输出的时候,不必担心封装的数据的不同而导致不能实现,只需重载Object中的toString()方法即可。

        例如:Object ob1=new Person("张三",19);

            Object ob2=new Pet("小花","花色",2);

    具体代码如下:

      

    package pro;
    
    class Link{
        class Node{
            private Object data;
            private Node next;
            public Node(Object ob){
                this.data=ob;
            }
            public void add(Node newNode){//节点中的增加方法
                if(this.next==null){
                    this.next=newNode;
                }
                else{
                    this.next.add(newNode);
                }
            }
            public void print(){
                System.out.print(this.data.toString()+"	");
                if(this.next!=null)
                    this.next.print();
            }
            public boolean search(Object data){
                if(data.equals(this.data)){
                    return true;
                }
                else{
                    if(this.next!=null)
                        return this.next.search(data);
                    else
                        return false;
                }
            }
            public void delete(Node pre,Object data){
                if(data.equals(this.data))
                    pre.next=this.next;
                else{
                    if(this.next!=null){
                        this.next.delete(this,data);
                    }
                }
            }
        };
        private Node root;
        public void addNode(Object data){
            Node newNode=new Node(data);
            if(this.root==null) 
                this.root=newNode;
            else
                this.root.add(newNode);
        }
        public void printNode(){
            if(this.root!=null){
                this.root.print();
            }
        }
        public boolean findNode(Object name){
            return this.root.search(name);
        }
        public void deleteNode(Object data){
            if(this.findNode(data)){
                if(this.root.data.equals(data)){
                    this.root=this.root.next;
                }
                else
                    this.root.next.delete(root,data);
            }
            else
                System.out.println("要删除的节点不存在");
        }
    };
    class Person{
        private String name;
        private int age;
        public Person(String name,int age){
            this.name=name;
            this.age=age;
        }
        public String toString(){
            return "姓名:"+this.name+" 年龄:"+this.age;
        }
    }
    class Pet{
        private String name;
        private String color;
        private int age;
        public Pet(String name,String color,int age){
            this.name=name;
            this.color=color;
            this.age=age;
        }
        public String toString(){
            return "宠物名:"+this.name+" 颜色:"+this.color+" 年龄:"+this.age;
        }
    }
    public class Link01{
        public static void main(String args[]){
            Link H=new Link();
            Object ob1=new Person("张三",19);
            Object ob2=new Pet("大黄","黄色",2);
            Object ob3=new Person("李四",20);
            Object ob4=new Pet("小黑","黑色",3);
            Object ob5=new Person("王五",30);
            H.addNode(ob1);
            H.addNode(ob2);
            H.addNode(ob3);
            H.addNode(ob4);
            H.addNode(ob5);
            H.addNode("F");
            H.printNode();
            System.out.println();
            H.deleteNode(ob2);
            H.deleteNode(ob4);
            H.printNode();
            System.out.println();
            System.out.println(H.findNode(ob1));
            H.deleteNode("X");
    
        }
    }

        

  • 相关阅读:
    水平触发与边缘触发
    Leetcode935 骑士拨号器
    leetcodeF47 礼物的最大价值
    leetcodeF42 连续子数组的最大和
    leetcode12 矩阵中的路径 回溯算法
    leetcode14-II 剪绳子II DP 解法配合快速乘取模
    leetcode17.16 按摩师DP
    leetcode530 二叉树的最小绝对差
    PCB genesis大孔加小孔(即卸力孔)实现方法
    PCB genesis短槽加引导孔实现方法
  • 原文地址:https://www.cnblogs.com/hxtblogs/p/7711377.html
Copyright © 2011-2022 走看看