Java achieve a one-way linked list

Explanation

The simplest idea list: A.add (B); B.add ( C); C.add (D);
Each node maintains its own data and a reference to the next node.
example:

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

Above implements a very simple linked list, the user must manually set the reference to the relationship between every two nodes will be very troublesome. So the best operating node will encapsulate, let's achieve increased, find, delete node operation.

Packaging classes:

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:

Before deleting ======= ========
ABCDE
========= ====== After deleting
ABDE
query node: true

Published 22 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_19408473/article/details/72385460