The realization of headless bidirectional linked list

1. head insertion

public void addFirst(int data) {  //Head insertion
        DLinkedNode newNode = new DLinkedNode(data);//New nodes joined
        DLinkedNode next = head.next;
        newNode.next = next;
        next.prev = newNode;
        head.next = newNode;
        newNode.prev = head;
    }

2. tail insertion method

public void addLast(int data) {//Tail insertion
        DLinkedNode newNode = new DLinkedNode(data);//Newly inserted node
        DLinkedNode prev = head.prev;
        newNode.next = head;
        head.prev = newNode;
        newNode.prev = prev;
        prev.next = newNode;
    }

3. Insert anywhere, the first data node is subscript 0

public void addIndex(int index,int data) {//Insert anywhere
        int size = size();
        if(index < 0 || index > size){
            return;
        }
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size) {
            addLast(data);
            return;
        }
        DLinkedNode next = getPos(index);
        DLinkedNode prev = next.prev;
        DLinkedNode newNode = new DLinkedNode(data);
        prev.next = newNode;
        newNode.prev = prev;
        next.prev = newNode;
        newNode.next = next;
    }

The method used here to calculate the length of the linked list:

public int size(){
        int size = 0;
        for(DLinkedNode cur = head.next; cur != head; cur = cur.next) {
            size++;
        }
        return size;
    }

And find a location in the linked list:

public DLinkedNode getPos(int index) {
        DLinkedNode cur = head.next;
        for(int i = 0; i < index; i++){
            cur = cur.next;
        }
        return cur;
    }

4. Check whether the keyword key is in the linked list

public boolean contains(int key) {//Find the node containing the key
        for(DLinkedNode cur = head.next; cur != head; cur = cur.next) {
            if(cur.val == key){
                return true;
            }
        }
        return false;
    }

5. Delete the node with the first key

public void remove(int key){
        DLinkedNode toRemove = find(key);
        if(toRemove == null) {
            return;
        }
        DLinkedNode prev = toRemove.prev;
        DLinkedNode next = toRemove.next;
        prev.next = next;
        next.prev = prev;
    }

Here we use the method to find the position of key in the linked list:

public DLinkedNode find(int key) {
        for(DLinkedNode cur = head.next; cur != head; cur = cur.next) {
            if(cur.val == key){
                return cur;
            }
        }
        return null;
    }

6. Delete all nodes whose value is key

public void removeAll(int key){
        while (true){
            DLinkedNode toRmove = find(key);
            if(toRmove == null){
                return;
            }
            DLinkedNode prev = toRmove.prev;
            DLinkedNode next = toRmove.next;
            prev.next = next;
            next.prev = prev;
        }
    }

7. Print the linked list

public void display(){
        System.out.print("Forward:[");
        for(DLinkedNode cur = head.next; cur != head; cur = cur.next){
            System.out.print(cur.val);
            if(cur.next != head){
                System.out.print(",");
            }
        }
        System.out.println("]");
        System.out.println("reverse:[");
        for(DLinkedNode cur = head.prev; cur != head; cur = cur.prev){
            System.out.print(cur.val);
            if(cur.prev != head){
                System.out.print(",");
            }
        }
        System.out.println("]");
    }

8. Clear the list

public void clear(){
        head.next = head;
        head.prev = head;
    }

Tags: Java

Posted on Wed, 04 Dec 2019 17:14:33 -0500 by devsanctum