Java data structure and algorithm (5) - bidirectional linked list

Keywords: git

  • What is double linked list
    Each node not only saves the reference of the next node in xui, but also the reference of the previous node.
  • From the head
    To judge the linked list, if it is empty, this is the node added by the end node for the message. If it is not empty, you need to set the previous node of the cast node as the heart node.
  • Insert from tail
    If the link list is empty, the head node is directly set as the newly added node, otherwise the next node of the tail node is set as the newly added node. At the same time, the former node of the newly added node is set as the tail node.
  • Delete from head
    Determine whether the header node has the next node. If not, set it to null. Otherwise, set the previous of the next node of the header node to null.
  • Delete from tail
    If there is no other node after the head node, set the tail node to null. Otherwise, set the next of the previous node of the tail node to null. Set the tail node as its previous node.
  • Delete method
    There is no need to use another pointer field at 0.
package com.fantj.dataStruct.doublelistnode;

/**
 * Double linked list, which has one more head node than double ended list
 * Created by Fant.J.
 * 2017/12/21 19:49
 */
public class DoubleLinkList {
    //Head node
    private Node first;
    //Caudal node
    private Node last;

    public DoubleLinkList(){
        first = null;
    }
    /**
     * Insert a node after the head node
     */
    public void insertFirst(long value){
        Node node = new Node(value);
        //If it is the first time to insert
        if (isEmpty()){
            last = node;
        }else {
            first.previous = node;
        }
        node.next = first;
        first = node;
    }
    /**
     * Insert a node from the tail node
     */
    public void insertLast(long value){
        Node node = new Node(value);
        if (isEmpty()){
            first = node;
        }else {
            last.next = node;
            node.previous = last;
        }
        last = node;
    }
    /**
     * Delete a node after the header node
     */
    public Node deleteFirst(){
        Node temp = first;
        if (first.next == null){
            last = null;
        }else {
            first.next.previous = null;
        }
        first = temp.next;
        return temp;
    }
    /**
     * Display method
     */
    public void display(){
        Node current = first;
        while (current != null){
            current.display();   //Print node
            current = current.next;
        }
    }
    /**
     * Search method
     */
    public Node find(long value){
        Node current = first;
        while (current.data != value){
            if (current.next == null){
                return null;
            }
            current = current.next;//Keep looking down
        }
        return current;
    }
    /**
     * Delete method, delete according to data field
     */
    public Node delete(long value){
        Node current = first;
        Node previous = first;//Represents the previous node
        while (current.data != value){
            if (current.next == null){
                return null;
            }
            previous = current; //Extract the current node as the previous node (use the next node of the node to point to the next node of the deleted node)
            current = current.next; //Keep looking down
        }
        if (current == first){
            first = first.next;
        }else {
            previous.next = current.next;
        }
        return current;
    }
    /**
     * Judge whether it is empty
     */
    public boolean isEmpty(){
        return (first == null);
    }
}
package com.fantj.dataStruct.doublelistnode;

/**
 * Chain structure
 * Created by Fant.J.
 * 2017/12/19 22:19
 */
public class Node {
    //Data field
    public long data;
    //Node domain (pointer domain)
    public Node next;
    public Node previous;


    public Node(long value){
        this.data = value;
    }

    /**
     * Display method
     */
    public void display(){
        System.out.print(data+" ");
    }
}

View source code: git address

Posted by gsb on Fri, 01 May 2020 11:44:35 -0700