Detailed explanation of the addition, deletion and modification of linked list for Java description of data structure learning
preface
Linked list is a common basic data structure. It is a linear list, but it is not stored sequentially in memory. It is stored in a chain. In each node, the "pointer" of the next node is stored. Data in Java is divided into reference data type and basic data type. There is no concept of pointer in Java, but for linked list, pointer refers to the address of reference data type.
Linked lists and arrays are linear data structures. For arrays, their length is fixed. Because they are continuous in memory, they are more suitable for search and traversal. Linked lists are not stored sequentially in memory, but they are composed of "pointers", so it is more convenient to insert and delete arrays.
The following code implements a simple linked list data structure described in Java language through internal classes and recursion. I won't say more below. Let's take a look at the detailed introduction together
Definition of linked list data structure
First, let's take a look at the definition of linked list data structure. The code is as follows:
For the definition of linked list, the nodemanager class is used to manage linked list operations, and the member internal class node is used to provide linked list data and linked structure. For the users of the class, they do not directly access data, so the operation is the nodemanager class, and the internal class node provides real data management. Therefore, the node class needs to provide real data operation methods, and the nodemanager class also needs to provide a set of methods for external operation of linked lists. Therefore, the seemingly same methods are provided in the nodemanager class and the node class, but the actual meaning is different.
Let's view the add () method in nodemanager class and node class. The code is as follows:
The above method in the code is the method in the nodemanager class, while the following method is the method in the node class.
A root member variable is provided in the manager class, which is used to manage the head node of the linked list. Therefore, when adding a node, you will first judge whether the root is empty. If it is empty, you will directly save the node by root. If the root is not empty, you can add it through the addnode() method in the node class. The idea is to find the last node in the current linked list, The new node is added to the next member variable assigned to the last node.
Addition, deletion, modification and query of linked list
The same idea applies to other operations of the linked list. The complete codes for the addition, deletion, modification, query and output of the linked list are as follows:
The above is the complete code of the basic operation of the linked list. Write a calling code to test. The code is as follows:
Compile and run the code, and the results are as follows:
For the collection classes in Java, you use a lot of knowledge of data structures. When you are in a good state, learn the source code of Java collection classes. I will try to be a junior programmer!
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.