Linked list of data structure and Java implementation

Keywords: Java

1, Basic introduction of linked list

Linked list is the most basic data structure. The implementation principle of linked list in Java is linked list. The linked list is composed of multiple nodes, each node only stores data and the location of the next node, so that multiple nodes can be strung together level by level, and the last node points to null. Therefore, the linked list does not need continuous memory space, and the storage is distributed. Each node is connected through the address of each node. There is only one head node exposed to the outside of the linked list. The whole linked list can be operated by the head node. The efficiency of the linked list is not high when it is traversed circularly, but the advantages of inserting and deleting are obvious.

2, Java implementation of linked list

public class Link {

    class Node{
        Node next = null;//Point to next node
        int data;//Node value
        public Node(int data){
            this.data = data;
        }
    }

    Node head = null;//Define chain header node


    //Add data to linked list
    public void addNode(int data){
        Node newNode = new Node(data);
        if(head == null){
            head = newNode;
            return;
        }
        newNode.next = head;
        head = newNode;
    }
    //Adding nodes to a linked list
    public void addNode(Node n){
        if(head == null){
            head = n;
        }
        n.next = head;
        head = n;
    }
    //Return the length of the linked list
    public int length(){
        int num = 1;
        if(head == null){
            return 0;
        }
        Node tmp = head;
        while(tmp.next!=null){
            num++;
            tmp = tmp.next;
        }
        return num;
    }
    //Delete the index node
    public boolean delNode(int index){
        if(index<1||index>length()){
            return false;
        }
        if(index == 1){
            head = head.next;
            return true;
        }
        int i = 1;
        Node preNode = head;
        Node curNode = head.next;
        while(curNode!=null){
            i++;
            if(index == i){
                preNode.next = curNode.next;
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
        }
        return false;
    }
    //Print data in linked list
    public void printLink(){
        Node tmp = head;
        while(tmp.next!=null){
            System.out.print(tmp.data + " ");
            tmp = tmp.next;
        }
        System.out.print(tmp.data);
        System.out.println();
    }
    //Delete a node
    public boolean delNode(Node n){
        if(n == null){
            return false;
        }
        if(n == head){
            head = head.next;
            return true;
        }
        Node preNode = head;
        Node curNode = head.next; 
        while(curNode != null){
            if(curNode == n){
                preNode.next = curNode.next;
                return true;
            }
            preNode = curNode;
            curNode = curNode.next;
        }
        return false;

    }
    //Delete a node without knowing the head node
    public boolean deleteNode(Node n){
        if(n == null){
            return false;
        }
        if(n.next ==  null){
            n = null;
            return true;
        }
        n.data = n.next.data;
        n.next = n.next.next;
        return true;
    }
    public static void main(String args[]){
        Link link = new Link();
        link.addNode(1);
        link.addNode(2);
        link.addNode(3);
        link.addNode(4);
        link.printLink();
        link.addNode(5);
        link.printLink();
        System.out.println("length: " + link.length());
        System.out.println("del index 3: " + link.delNode(3));
        link.printLink();
        System.out.println("del index 4: " + link.delNode(4));
        link.printLink();
    }
}

Output content:

4 3 2 1
5 4 3 2 1
length: 5
del index 3: true
5 4 2 1
del index 4: true
5 4 2

Posted by jamesflynn on Tue, 31 Mar 2020 04:06:01 -0700