Re learning data structure — single linked list

Time:2020-11-27

Linked list

brief introduction

  • Linked list is a very important data structure in data structure. It is not only important in itself, but also an important part of data structure such as tree and graph.
  • There are many kinds of linked lists: one-way list, circular list, two-way list and so on.
  • Linked list is a kind of sequential structure which is distributed randomly in memory and linked by memory address through pointer. Because of its special memory distribution characteristics,For delete and add operations, it is more efficient than array. The efficiency of searching and modifying is low

Let’s learn how to add, delete, and modify some single linked lists.

Function realization

For the linked list can be said to love and hate, love is because it is really easy to use, hate is because it is difficult to understand, back and forth to learn several times to understand.

Define single linked list node class

The linked list consists of two parts: data + address pointer。 Generally, the head node does not store data, but only serves as the starting index of a single linked list to facilitate related operations (the head node does not necessarily exist). The address pointer stores the memory address of the next node, and the write pointer address of the last node is null. The data part can be defined on demand, where the data part is composed of number (no) and name (name).

  • The code is as follows:
/**
 *Hero node class
 * @author laona
 */
class HeroNode {

    /**
     *Node number
     */
    int no;
    /**
     *Node information
     */
    String name;
    /**
     *Next node
     */
    HeroNode next;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }
    
    //The toString method is overridden to facilitate traversing the linked list
    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

Implementation of one way linked list

Define an empty head node. The head node only needs to store the memory address of the next node. The next = = null of the empty chain header node

  • The implementation code is as follows:
/**
 *Single chain list class
 * @author huaian
 */
class SingleLinked {
    /**
     *Empty head node, next = = null
     */
    private HeroNode headNode = new HeroNode(0, "");
}

Add nodes at the end of the list

Add a new node to the linked list. The most important thing is to find the end node of the list (that is, the last node in the list points to an empty node), which requires traversing the list.

Traversing the linked list requires a temporary variable to obtain the current non empty node temp of the linked list until the last one is empty.

  • The circulatory conditions are as follows:
//Get next node
temp = temp.next
  • Add new node statement as follows:
temp.next = newNode;
  • The function implementation code is as follows:
/**
 *Add a new node to the single linked list
 *@ param newnode {@ link heronode} new node
 */
public void addNode(HeroNode newNode) {
    HeroNode temp = headNode;
    while (true) {
        if (temp.next == null) {
            break;
        }
        temp = temp.next;
    }
    temp.next = newNode;
}

The above addition function is completed. Can the data in the linked list be arranged in the order of no? It must be possible.

Add nodes in ascending order

Sorting in ascending order is not difficult. The implementation code is similar to that of adding nodes at the end of the list. A key point to understand is: the linked list can only get the next node through the previous node’s next, then the passed in no and the previous node’s next.no Comparison.

  • The comparison conditions were as follows
temp.next.no >= newNode.no
  • The function code is as follows:
/**
 *Add nodes in ascending order of number
 *@ param newnode {@ link heronode} new node
 */
public void addNodeByOrder(HeroNode newNode){
    HeroNode temp = headNode;
    while (true) {
        if (temp.next == null) {
            break;
        }
        if (temp.next.no >= newNode.no) {
            //Cannot have the same node
            if (temp.next.no == newNode.no) {
                System.out.printf ("an element with the same index already exists: no =% D, insert failed", newNode.no );
                break;
            }
            newNode.next = temp.next;
            temp.next = newNode;
            break;
        } else {
            temp = temp.next;
        }
    }
    temp.next = newNode;
}

Modify node information

To modify the node information is to compare whether the information of the next node of the current node is the same as that of the node to be modified. If it is compatible, it can be modified directly. If it is different, it will continue to compare the next node.

  • The function implementation code is as follows:
/**
 *Modify information according to node number
 *@ param no node number
 *@ param newname node information
 */
public void modifyByNo(int no, String newName) {
    HeroNode temp = headNode;
    while (true) {
        if (temp.next == null) {
            System.out.printf ("does not exist, node No.:% d '", No.);
            break;
        }
        if (temp.next.no == no) {
            temp.next.name = newName;
            break;
        }
        temp = temp.next;
    }
}

Delete node

With the foreshadowing, deleting nodes is easy. To delete a node, you need to specify which node to delete. Here, you can delete a node by No. here, you need to compare whether the information of the next node of the current node is the same as that of the node to be deleted. If the information is the same, delete it.

  • Delete code (by pointing the next of the current node to the next.next ):
//By pointing the current node's next to the current node's next.next
temp.next = temp.next.next;
  • The function code is as follows:
/**
 *Delete node by no
 *@ param no node number
 */
public void dropNodeByINo(int no) {
    HeroNode temp = headNode;
    while (true) {
        if (temp.next == null) {
            System.out.printf ("does not exist, node No.:% d '", No.);
            break;
        }
        if (temp.next.no == no) {
            temp.next = temp.next.next;
            break;
        }
        temp = temp.next;
    }
}

Traversing the linked list

Convenient to view the content of the linked list, write a function to traverse the list, the code is as follows:

/**
 *Traversing all nodes of a single linked list
 */
public void list() {
    HeroNode temp = headNode;
    if (headNode.next == null) {
        System.out.println (the linked list is empty ~! "";
        return;
    }
    while (true) {
        if (temp.next == null) {
            break;
        }
        System.out.println(temp.next);
        temp = temp.next;
    }
}

The whole of single chain list class

For the convenience of viewing, the whole code of singlelinked code is pasted here (save space, the function only adds the header, and the function body is omitted)

  • The code for the singlelinked class is as follows:
/**
 *Single chain list class
 * @author huaian
 */
class SingleLinked {
    private HeroNode headNode = new HeroNode(0, "");

    /**
     *Delete node by no
     *@ param no node number
     */
    public void dropNodeByINo(int no) {
       ······
    }

    /**
     *Modify information according to node number
     *@ param no node number
     *@ param newname node information
     */
    public void modifyByNo(int no, String newName) {
        ······
    }

    /**
     *Add nodes in ascending order of number
     *@ param newnode {@ link heronode} new node
     */
    public void addNodeByOrder(HeroNode newNode){
        ······
    }

    /**
     *Add a new node to the single linked list
     *@ param newnode {@ link heronode} new node
     */
    public void addNode(HeroNode newNode) {
        ······
    }

    /**
     *Traversing all nodes of a single linked list
     */
    public void list() {
        ······
    }
}

/**
 *Hero node class
 * @author huaian
 */
class HeroNode {

    /**
     *Node number
     */
    int no;
    /**
     *Node information
     */
    String name;
    /**
     *Next node
     */
    HeroNode next;

    //Structural transfer
    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

Summary

The core of single linked list is to get the information of the next node through the next of the previous node. To understand this, it is not difficult to list.

When searching and comparing, do you need to know whether to compare with the previous node or the current node? Is it the current node or the next node? wait.


If you are nameless, concentrate on practicing sword!
Like friends can leave your praise!