java 单向链表的实现

说明

最简单的链表思路:A.add(B);B.add(C);C.add(D);
每个节点保存自己的数据和对下一个节点的引用。
范例:

public class Node {

    private String data;
    private Node  next;

    public Node(String data) {
        this.data = data;
    }

    public void setNext(Node next) {
        this.next = next;
    }
    public Node getNext(){
        return this.next;
    }

    public String getData(){
        return this.data;
    }

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

test code :

    @Test
    public void test(){
       Node root = new Node("locomotive") ;
       Node n1 = new Node("car-A");
       Node n2 = new Node("car-B");
       Node n3 = new Node("car-C");

       root.setNext(n1);
       n1.setNext(n2);
       n2.setNext(n3);

       root.pringNode(root);
    }

run result:

locomotive car-A car-B car-C

上面实现了一个非常简单的链表,用户必须手动的去设置每两个节点之间的引用关系,会非常麻烦。所以最好将对节点的操作进行封装,下面我们来实现增加、查找、删除节点的操作。

封装的类:

public 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 privious,String data){
            if(data.equals(this.data)){
                privious.next = this.next;
            }else {
                if(this.next !=null){
                    this.next.delete(this,data);
                }
            }
        }
    };

    private Node root;
    public void addNode(String data){
        Node node  = new Node(data);
        if(this.root == null){
            this.root = node;
        }else {
            this.root.add(node);
        }
    }
    public void printNode(){
        if(this.root !=null){
            this.root.print();
        }
    }

    public boolean contains(String name){
        return  this.root.search(name);
    }

    public void deleteNode(String name){
        if(this.contains(name)){
            if(this.root.data.equals(name)){
                this.root = this.root.next;
            }else {
                this.root.delete(root,name);
            }
        }
    }

}

test code:

    @Test
    public void Test(){
        Link l = new Link() ;
        l.addNode("A") ;        // 增加节点
        l.addNode("B") ;        // 增加节点
        l.addNode("C") ;        // 增加节点
        l.addNode("D") ;        // 增加节点
        l.addNode("E") ;        // 增加节点
        System.out.println("======= 删除之前 ========") ;
        l.printNode() ;
        // System.out.println(l.contains("X")) ;
        l.deleteNode("C") ;     // 删除节点
//        l.deleteNode("D") ;       // 删除节点
//        l.deleteNode("A") ;       // 删除节点
        System.out.println("\n====== 删除之后 =========") ;
        l.printNode() ;
        System.out.println("\n查询节点:" + l.contains("B")) ;
    }

run result:

======= 删除之前 ========
A B C D E
====== 删除之后 =========
A B D E
查询节点:true

发布了22 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_19408473/article/details/72385460
今日推荐