链表实例link

博客给出了一个链表类Link的代码实现,包含节点类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 previous,String data){

            if(data.equals(this.data)){

                previous.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 contains(String name){

        return this.root.search(name);

    }

    public void deleteNode(String data){

        if(this.contains(data)){

            if(this.root.data.equals(data)){

                this.root = this.root.next;

            }else{

                this.root.next.delete(root,data);

            }

        }

    }

}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值