Data structure (java version) bidirectional circular list day(6)

I. two way circular list

1.1 bidirectional circular list

  • A reference to the previous node
  • A reference to the next node
  • A value area
public class DoubleNode {
    DoubleNode lastnode;
    DoubleNode nextnode;
    Object value;
}

1.2 add node

  • Get the next node of the current node
  • Connect the node to be added to the next node
  • Connect the node to be added to the previous node
//Add node
    public void addNode(DoubleNode node){
        //Next node
        DoubleNode nodeNext = this.next;
        node.next = nodeNext;
        nodeNext.last = node;
        this.next = node;
        node.last = this;
    }

1.4 delete the next node of the current node

public void delete(DoubleNode node){
        DoubleNode nextNode = this.next.next;
        this.next = nextNode;
        nextNode.last = this;
    }

1.5 delete the current node

public void deleteNow(){
        DoubleNode lastNode = this.last;
        DoubleNode nextNode = this.next;
        lastNode.next = nextNode;
        nextNode.last = lastNode;
    }

1.6 pay attention to

The last and next areas of the double linked list cannot be assigned as null, which may cause Nullpointer exceptions, which can make next and last point to the current node, and form a double-way circular list.

Two, test

Source code:

public class DoubleNode {
    DoubleNode last = this;
    DoubleNode next = this;
    Object value;
    public DoubleNode(Object value){
        this.value = value;
    }
    //Add node
    public void addNode(DoubleNode node){
        //Next node
        DoubleNode nodeNext = this.next;
        node.next = nodeNext;
        nodeNext.last = node;
        this.next = node;
        node.last = this;
    }
    //Delete the next node of the current node
    public void delete(DoubleNode node){
        DoubleNode nextNode = this.next.next;
        this.next = nextNode;
        nextNode.last = this;
    }
    //Delete current node
    public void deleteNow(){
        DoubleNode lastNode = this.last;
        DoubleNode nextNode = this.next;
        lastNode.next = nextNode;
        nextNode.last = lastNode;
    }
}

Test code:

public class demo3 {
    public static void main(String[] args) {

        DoubleNode node = new DoubleNode(1);
        DoubleNode node1 = new DoubleNode(2);
        DoubleNode node2 = new DoubleNode(3);
        DoubleNode node3 = new DoubleNode(4);
        node.addNode(node1);
        node1.addNode(node2);
        node2.addNode(node3);
        System.out.println(node1.next.value);
        System.out.println(node1.last.value);
        System.out.println("The value of the next node of the last node:");
        System.out.println(node3.next.value);
        node.delete(node1);
        System.out.println("---------------------");
        System.out.println(node.next.value);
        System.out.println(node.last.value);
        node.deleteNow();
        System.out.println("---------------------");
        System.out.println(node2.next.value);
        System.out.println(node2.last.value);
    }
}

//Result:
3
1
//Value of the next node of the last node:
1
---------------------
3
4
---------------------
4
4

Posted by mnewhart on Mon, 02 Dec 2019 06:44:45 -0800