[java data structure linked list]----The ultimate explanation that beginners will know at a glance, and veterans will cry when they see it

Directory Guide

linked list

      The concept and structure of linked list

      Implementation of linked list

         1. Print the linked list

        2. Get the length of the singly linked list

        3. Head plug method

        4. Tail insertion method

        5. Any position input. The first data node is subscript 0

        6. Check whether the linked list contains the keyword key

        7. Delete the node whose first occurrence of the keyword is key

        8. Delete all nodes whose value is key

        9. Clear the linked list

The concept and structure of linked list

A linked list is a non-contiguous storage structure on a physical storage structure, and the logical order of data elements is realized through the reference link order in the linked list.

  

In practice, the structure of linked lists is very diverse. There are 8 linked list structures in combination of the following situations:

 

1. One-way, two-way

2. To lead, not to lead

3. Cyclic, acyclic

singly linked list, doubly linked list

 

 Although there are so many linked list structures, we focus on two

Headless one-way acyclic linked list: The structure is simple, and it is generally not used to store data alone. In practice, it is more of a substructure of other data structures, such as hash buckets, graph adjacency lists, and so on. In addition, this structure appears a lot in the written test interview.

     

 

 Implementation of linked list

Before we implement the interface function of the linked list, we must first create two classes, the linked list class (including a variable header node and interfaces that implement various functions) and the node class (including member attributes value and next)

 

    

 The functions of the interfaces written below are implemented in the linked list class

 1. Print the linked list

     

 2. Get the length of the singly linked list

Refer to the local variable cur to traverse the linked list, and refer to a local variable count to count, as long as the node is not null, then count is +1, and the final count value returned is the length of the linked list.

 

3. Head plug method

The so-called head insertion method is to insert a node in front of the head node. First of all, we have to judge whether the head node is empty. If it is empty, then the new node created is directly the head node; if it is not empty, the address of the head node is first stored in the next of the new node. , and then set the new node as the head node. ( Note:  do not mess with the order!!!)

 4. Tail insertion method

The tail insertion method is different from the head insertion method. The tail insertion method must determine whether the linked list is empty for the first time (that is, to determine whether the head node is empty). If the head node is empty, the node to be inserted is the head node. Point, if it is not empty, introduce the local variable cur to traverse the linked list to find the last node. When cur.next is empty, it means that the last node has been found, and then let next in cur point to the node to be inserted! ! ! ! !

 5. Any position input. The first data node is subscript 0

First of all, we need to judge whether the insertion position is reasonable, and then if we want to insert a node at any position, we must first find the previous node of the insertion position (because this is a singly linked list, so it cannot be directly known), and then write a method to find the previous node where to insert.

 The picture is a little sloppy, I'm sorry to watch the officers, the level of Xiaosheng's painting is really not good!  …

 6. Check whether the linked list contains the keyword key

The keyword key is passed in, and the local variable cur is introduced to traverse the linked list. If it encounters the same element as the key during the traversal process, it returns true, otherwise it returns false.

 7. Delete the node whose first occurrence of the keyword is key

First of all, we need to judge whether the linked list is empty (that is, whether the head node is empty), and then we need to check whether the key node to be deleted is the head node. If it is the head node, then directly point the reference of the head node down. One node, the next node becomes the head node; if the key node to be deleted is not the head node, point the previous node of the key node to the next node of the key node

 8. Delete all nodes whose value is key

Its idea is similar to deleting the key node that appears for the first time, that is to jump out of the loop and return when a node to be deleted is found; and this is to traverse the entire linked list until all the key values ​​to be deleted are found before jumping out of the loop.

 9. Clear the linked list

First, refer to the variable curNext to save the next node of the head node. If it is not saved, then when the next node of the head node is set to empty, the next node of the head node will not be found, so it is impossible to store all other nodes. If it is set to empty, the linked list cannot be cleared.

 The last difference 

How about giving a compliment to your little brother, old irons? I think it's very good!

 

Guess you like

Origin blog.csdn.net/Biteht/article/details/121583883