可复用的链表

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

思路:写一个线性表类,在类中实现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+"\t");
            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()+"\t");
            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");

    }
}

 

 

    

转载于:https://www.cnblogs.com/hxtblogs/p/7711377.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值