Data structure - linked list

Pay attention to WeChat's official account of YiFan, record simple notes and make your favorite.

Features of linked list

1. It is stored as a node,

2. Each node contains data field, next field: points to the next node

3. Each node of the linked list is not necessarily stored continuously

4. The chain list is divided into the chain list with the lead node and the chain list without the lead node

Single chain table

characteristic

A chained access data structure in which data elements in a linear table are stored in a set of storage units with arbitrary addresses. The data in the linked list is represented by nodes. The composition of each node: element (the image of data element) + pointer (indicating the storage location of subsequent elements). The element is the storage unit for storing data, and the pointer is the address data connecting each node

code

package com.yifan.linear.Linkedlist;

/**
 * @Author YIFan
 * @Date 2020/6/7 15:49
 * @Version 1.0
 */
// One way list
public class Node {

    // Node content
    private int data;
    // Next node
    private Node next;

    public Node(int data) {
        this.data = data;
    }

    // Append node
    public Node apped(Node node){
        // Get current node
        Node currentNode = this ;
        // Loop back
        while (true){
            // Take out the next node
            Node nextNode = currentNode.next;
            // The next node is null the current node is the last one
            if(nextNode == null){
                break;
            }
            // Assign next node to current node
            currentNode = nextNode;
        }
        // Append the node to be appended as the next node to find the current node
        currentNode.next = node ;

        return this;
    }

    // Get next node
    public Node next(){
        return this.next;
    }

    // Get data from node
    public int getData() {
       return this.data;
    }

    // Judge whether the node is the last node
    public boolean isLast(){
        return next == null;
    }

    // Show nodes
    public void show(){
        Node currentNode = this;
        while (true){
            System.err.print(currentNode.getData()+" -> ");
            currentNode = currentNode.next;
            if(currentNode == null ){
                System.err.print("null ");
                break;
            }
        }
        System.err.println();
    }

    // Delete the next node of the current node
    public void removeNext(){
        // Only the next node of the current node can be deleted to get the next node deleted
        Node node = next.next ;
        // Set the next node as the next node of the current node
        this.next = node;
    }

    // Insert a node
    public void afterNode(Node node){
        // Take the next node of the current node as the next node
        Node nextNext = this.next;
        // Assign the inserted node to the next node of the current node
        this.next = node;
        //Set the next node as the next node of the new node
        node.next = nextNext;
    }
}

public class TestNode {

    public static void main(String[] args) {
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        // Append node
        node1.apped(node2).apped(node3);

        node1.show();
        node1.afterNode(node4);
        node1.show();
    }
}

Circular list

characteristic

Circular list is another form of chain storage structure. Its characteristic is that the pointer field of the last node in the table points to the head node, and the whole linked list forms a ring

When a circular list has only one node, its next node points directly to itself

code

package com.yifan.linear.Linkedlist;

/**
 * @Author YIFan
 * @Date 2020/6/7 15:49
 * @Version 1.0
 */
// Circular list
public class CircleNode {

    // Node content
    private int data;
    // Next node
    private CircleNode next = this;

    public CircleNode(int data) {
        this.data = data;
    }

    // Get next node
    public CircleNode next(){
        return this.next;
    }

    // Get data from node
    public int getData() {
       return this.data;
    }

    // Judge whether the node is the last node
    public boolean isLast(){
        return next == null;
    }


    // Delete the next node of the current node
    public void removeNext(){
        // Only the next node of the current node can be deleted to get the next node deleted
        CircleNode node = next.next ;
        // Set the next node as the next node of the current node
        this.next = node;
    }

    // Insert a node
    public void afterNode(CircleNode node){
        // Take the next node of the current node as the next node
        CircleNode nextNext = this.next;
        // Assign the inserted node to the next node of the current node
        this.next = node;
        //Set the next node as the next node of the new node
        node.next = nextNext;
    }
}

public class TestNode {

    public static void main(String[] args) {

        CircleNode node1 = new CircleNode(1);
        CircleNode node2 = new CircleNode(2);
        node1.afterNode(node2);
        System.out.println(node1.next().getData()); --> 1 -> 2
        System.out.println(node2.next().getData()); --> 2 -> 1

    }
}

Two way circular list

introduce

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. In general, we construct two-way cyclic list

Sketch Map

code implementation

package com.yifan.linear.Linkedlist;

/**
 * @Author YIFan
 * @Date 2020/6/12 11:21
 * @Version 1.0
 */
// Circular bidirectional list
public class CircleDoubleNode {
    // If only one node is created, the previous node and the next node are all his own
    // Previous node
    private CircleDoubleNode per = this;
    // Next node
    private CircleDoubleNode next = this;
    // Node data
    private int data ;

    public CircleDoubleNode(int data){
        this.data = data;
    }

    //Add node
    public void after(CircleDoubleNode node){
        // Original next node
        CircleDoubleNode nextNext = this.next;
        // Treat the new node as the next node of the current node
        this.next = node;
        // Treat the current node as the previous node of the new node
        node.per = this;
        // Make the original next node the next node of the new node
        node.next = nextNext;
        // Let the previous node of the original next node be the new node
        nextNext.per = node;
    }

    //Next node
    public CircleDoubleNode next(){
        return this.next;
    }

    //Previous node
    public CircleDoubleNode previous(){
        return this.per;
    }

    //get data
    public int getData(){
        return this.data;
    }

}
public class TestNode {

    public static void main(String[] args) {


       CircleDoubleNode node1 = new CircleDoubleNode(1);
       CircleDoubleNode node2 = new CircleDoubleNode(2);
       CircleDoubleNode node3 = new CircleDoubleNode(3);

       node1.after(node2);
       node2.after(node3);

       System.out.println(node2.previous().getData());  1
       System.out.println(node2.getData());  2
       System.out.println(node2.next().getData());  3

    }
}

Let's pay close attention to the WeChat official account of "Yi Fan".

Posted on Sun, 14 Jun 2020 04:34:41 -0400 by JasonTC