Data structure and algorithm Java implementation of double linked list

Keywords: Java

1. Data structure and algorithm (5) Java implementation of bidirectional linked list

1.1 what is a two-way linked list

Double linked list is also called double linked list. It is a kind of linked list. There are two pointers in each data node, pointing to direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its predecessor and successor nodes. Generally, we construct a bidirectional circular list.

  1. value field – the data field where the node values are stored
  2. prev domain - the pointer domain (chain domain) that holds the direct predecessor address (location) of the node
  3. next domain - the pointer domain (chain domain) that holds the direct successor address (location) of the node

The bidirectional linked list is formed by using prev and next fields to connect the nodes defined above.

1.2 JAVA implementation of bidirectional linked list

The CustomDoublyLinkedList class is a custom implemented two-way linked list.

package com.yuanxw.datastructure.chapter5;

/**
 * Custom double linked list
 * @param <E>
 */
public class CustomDoublyLinkedList<E> {
    /**
     * Node data structure
     * Node Internal class, external cannot access
     *
     * @param <E>
     */
    private static class Node<E> {
        // Node node value value
        private E value;
        // Previous Node
        private Node<E> prev;
        // Next Node
        private Node<E> next;
    }


    // Head node element
    private Node<E> first;
    // Tail node element
    private Node<E> last;
    // Custom LinkedList size
    private int size;


    /**
     * Add head node element
     *
     * @param element
     * @return
     */
    public boolean addFirst(E element) {
        Node newNode = new Node();
        newNode.value = element;
        newNode.next = first;
        if (isEmpty()) {
            last = newNode;
            first = newNode;
        } else {
            first.prev = newNode;
            newNode.next = first;
        }
        first = newNode;
        size++;
        return true;
    }

    /**
     * Add element to tail node
     * @param element
     * @return
     */
    public boolean addLast(E element) {
        Node newNode = new Node();
        newNode.value = element;
        if (isEmpty()) {
            last = newNode;
            first = newNode;
        } else {
            newNode.prev = last;
            last.next = newNode;
        }
        last = newNode;
        size++;
        return true;
    }

    /**
     * Get the last element
     * @return
     */
    public E getLast(){
        return last.value;
    }

    /**
     * Get the first element
     * @return
     */
    public E getFirst(){
        return first.value;
    }


    /**
     * Judge whether it is empty
     *
     * @return
     */
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * Include specified element or not
     * @param element
     * @return
     */
    private boolean contains(E element) {
        Node<E> currentNode = this.first;
        while (currentNode != null){
            if(currentNode.value.equals(element)){
                return true;
            }
            currentNode = currentNode.next;
        }
        return false;
    }

    /**
     * Gets the next element of the specified element
     * @param element
     * @return
     */
    private E getNext(E element){
        Node<E> currentNode = this.first;
        while (currentNode != null){
            if(currentNode.value.equals(element) && currentNode.next != null){
                return currentNode.next.value;
            }
            currentNode = currentNode.next;
        }
        return null;
    }

    /**
     * Gets the previous element of the specified element
     * @param element
     * @return
     */
    private E getPrev(E element){
        Node<E> currentNode = this.first;
        while (currentNode != null){
            if(currentNode.value.equals(element)){
                return currentNode.prev.value;
            }
            currentNode = currentNode.next;
        }
        return null;
    }

    /**
     * Delete specified element
     * @param element
     * @return
     */
    public boolean remove(E element){
        Node<E> currentNode = this.first;
        boolean flag = false;
        while (currentNode != null){
            if(currentNode.value.equals(element)){
                currentNode.prev.next = currentNode.next;
                currentNode.next.prev = currentNode.prev;
                flag = true;
            }
            currentNode = currentNode.next;
        }
        return flag;
    }

    /**
     * Rewrite toString method for easy display
     *
     * @return
     */
    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        if (this.isEmpty()) {
            buffer.append("[]");
            return buffer.toString();
        }
        buffer.append("[");
        Node<E> currentNode = this.first;
        while (currentNode != null) {
            buffer.append(currentNode.value).append(",");
            currentNode = currentNode.next;
        }
        buffer.deleteCharAt(buffer.length() - 1);
        buffer.append("]");
        return buffer.toString();
    }

    public static void main(String[] args) {
        CustomDoublyLinkedList customDoublyLinkedList = new CustomDoublyLinkedList();
        System.out.println("addLast()Add chain header node element Xizhimen: " + customDoublyLinkedList.addLast("Xizhimen"));
        System.out.println("addLast()Add chain header node element Gulou Street: " + customDoublyLinkedList.addLast("Drum Tower Street"));
        System.out.println("addLast()Add chain header node element Andingmen: " + customDoublyLinkedList.addLast("Andingmen"));
        System.out.println("addLast()Add chain header node element Dongzhimen: " + customDoublyLinkedList.addLast("Dongzhimen"));
        System.out.println("addLast()Add chain header node element East four ten: " + customDoublyLinkedList.addLast("Dongsishitiao"));
        System.out.println("------------------------------------------------------------------------------------");
        System.out.println("addFirst()Add chain header node element Chegongzhuang: " + customDoublyLinkedList.addFirst("Che Gong Zhuang"));
        System.out.println("addFirst()Add link header node element Fuchengmen: " + customDoublyLinkedList.addFirst("Fuchengmen"));
        System.out.println("addFirst()Add link header node element Fuxing gate: " + customDoublyLinkedList.addFirst("Fuxing gate"));
        System.out.println("------------------------------------------------------------------------------------");

        System.out.println("getFirst()Get the first element: " + customDoublyLinkedList.getFirst());
        System.out.println("getLast()Get the last element: " + customDoublyLinkedList.getLast());
        System.out.println("------------------------------------------------------------------------------------");
        System.out.println("View list customDoublyLinkedList Object all node elements:" + customDoublyLinkedList);

        System.out.println("getPrev()Specify the previous element of Fuchengmen element: " + customDoublyLinkedList.getPrev("Fuchengmen"));
        System.out.println("getNext()Specify the next element of Fuchengmen element: " + customDoublyLinkedList.getNext("Fuchengmen"));
        System.out.println("------------------------------------------------------------------------------------");

        System.out.println("remove()Delete the specified element: " + customDoublyLinkedList.remove("Andingmen"));
        System.out.println("View list customDoublyLinkedList Object all node elements:" + customDoublyLinkedList);
        System.out.println("------------------------------------------------------------------------------------");
    }
}

Execution result:

addLast() add chain header node element Xizhimen: true
 addLast() add chain header node element Gulou Street: true
 addLast() add chain header node element Andingmen: true
 addLast() add chain header node element Dongzhimen: true
 addLast() add chain header node element [East four ten]: true
------------------------------------------------------------------------------------
addFirst() add chain header node element Chegongzhuang: true
 addFirst() add chain header node element Fuchengmen: true
 addFirst() add link header node element Fuxing gate: true
------------------------------------------------------------------------------------
getFirst() gets the first element: Fuxing gate
 getLast() gets the last element: East four ten
------------------------------------------------------------------------------------
View and list all node elements of the customDoublyLinkedList object: [Fuxingmen, Fuchengmen, Chegongzhuang, Xizhimen, Gulou Street, Andingmen, Dongzhimen, dongshitiao]
getPrev() specifies the last element of Fuchengmen element: Fuxingmen
 getNext() specifies the next element of Fuchengmen element: Chegongzhuang
------------------------------------------------------------------------------------
remove() delete the specified element [East four ten]: true
 View and list all node elements of the customDoublyLinkedList object: [Fuxingmen, Fuchengmen, Chegongzhuang, Xizhimen, Gulou Street, Dongzhimen, dongshitiao]
------------------------------------------------------------------------------------

                          . Thank you for your attention.

——Yunxw

123 original articles published, 164praised, 470000 visitors+
His message board follow

Posted by EagerWolf on Mon, 17 Feb 2020 02:31:49 -0800