Handwrite a generic doubly linked list

foreword

In the context of the current environment, an interview is not considered a qualified interview if you don’t ask about algorithms ( ), and data structures closely related to algorithms are often asked, such as 集合, 链表, , , , , 队列, 矩阵and so on.

Does it feel difficult as follows:

  • Collection : as long as you have it
  • Linked list : simple and simple
  • Queues : basic operations
  • Binary tree : okay
  • Balanced binary tree : mediocre
  • Red-black tree : a bit difficult
  • Heap/Stack : Increased Difficulty
  • Picture : Today is the high-end bureau

It is still a bit difficult to come down with such a set of combination punches. In this article, we will first write 简简单单the linked list by hand. There are one-way linked list and two-way linked list in the linked list. Can you know the two-way linked list or not the one-way linked list? Go directly to the two-way linked list.

attribute definition

The attribute content of the doubly linked list must have the upper node prevand the lower node . We use generic definitions for the attributes. The attribute content of such a doubly linked list is as follows:nextdata

    private class Node<T>{
    
    

        private Node prev;
        private T data;
        private Node next;

        public Node(LinkedTable.Node prev,T data,LinkedTable.Node next){
    
    
            this.prev = prev;
            this.data = data;
            this.next = next;
        }

    }

The above is the structure of the storage node. The actual external class needs to set the head node and the tail node, so that you can directly choose to traverse from the beginning or from the end.

    public Node headNode;
    public Node tailNode;

ADD method

addThe method has no return value. If there is no return value, 有参构造函数the attribute content of the class is empty when entering add for the first time, which is headNodethe heel above tailNode.

The first step of add : first create a Node object according to the content of add, the previous node is the current tail node, and the next node does not

    private void add(T data) {
    
    
        Node node = new Node<>(tailNode,data,null);
    }

The second step of add : Initialize when it is judged headNodethat both are emptytailNode

    private void add(T data) {
    
    
        Node node = new Node<>(tailNode,data,null);
        if (headNode == null && headNode == null){
    
    
            headNode = node;
            tailNode = node;
        }
    }

The third step of add : determine whether the tail node is empty, if it is not empty, point the next of the tail node to the creation node, and replace the tail node with the current node

    private void add(T data) {
    
    
        Node node = new Node<>(tailNode,data,null);
        if (headNode == null && headNode == null){
    
    
            headNode = node;
            tailNode = node;
        }else{
    
    
            if (tailNode != null){
    
    
                tailNode.next = node;
            }
            tailNode = node;
        }
    }

Circulate the add method 100 times for verification, as shown in the figure below:

insert image description here
The tail node records the last 99 of the loop, and the head node records the first 0 of the loop

DELETE method

The first step of delete : define a local variable to refer to the head node, without affecting the node position of the head and tail

    private void delete(T data) {
    
    
        Node now = headNode;
    }

The second step of delete : whileloop to determine nowwhether the node is not empty

    private void delete(T data) {
    
    
        Node now = headNode;
        while (now != null){
    
    
        
        }
    }

The third step of deletenow : judge whether the node value is equal to the parameter value in the loop data. If the pointer of the previous node nextpoints to the current next node, it means that the grandfather directly points to the grandson, and the father is deleted, and then returns directly. Otherwise, the current node points to the next node to continue the cycle.

    private void delete(T data) {
    
    
        Node now = headNode;
        while (now != null){
    
    
            if (now.data == data){
    
    
                now.prev.next = now.next;
                return;
            }
            now = now.next;
        }
    }

GET method

Now that the data is put, it must be taken out intact, define a get method, the code is the same as the deletion above, it is nothing more than modifying the third step

    private T get(T data){
    
    
        Node<T> now = headNode;
        while (now != null){
    
    
            if (now.data == data){
    
    
                return now.data;
            }
            now = now.next;
        }
        return null;
    }

SET method

setThe method is regarded as overwriting update, set the content of the specified location, this step needs to be indexmarked.

    private boolean set(Integer index, T data){
    
    
        Node<T> now = headNode;
        AtomicInteger i = new AtomicInteger(0);
        while (now != null){
    
    
            if (i.getAndAdd(1) == index){
    
    
                now.data = data;
                return true;
            }
            now = now.next;
        }
        return false;
    }

verify:

First addan Mapobject and then adda intvalue, so that the first bit of the linked list is Map对象, and then execute set方法to modify the first bit Map对象to inta value8546

    public static void main(String[] args) {
    
    
        LinkedTable table = new LinkedTable();
        HashMap hashMap = new HashMap(){
    
    {
    
    
           put("哈喽","xxx");
        }};
        table.add(hashMap);
        table.add(1);
        System.out.println(table);
        table.set(0,8546);
        System.out.println(table);
    }

The first breakpoint : currently the first node object property is stillMap

insert image description here

Second breakpoint : Now the first node object property becomesInteger

insert image description here

The above completes a doubly linked list basedcrud

Guess you like

Origin blog.csdn.net/AnNanDu/article/details/126618115