5 minutes to learn the linear linked list in the data structure

foreword

In addition to some algorithms, we also need to master some common data structures, such as arrays, linked lists, stacks, queues, trees and other structures. In the previous article, I have taken you to learn about one-dimensional arrays and multidimensional arrays in Java, so I won't go into details about them. Next, I will explain to you the linked list in the linear structure, I hope you like it.


The full text is about [ 3200] words, no nonsense, just pure dry goods that allow you to learn techniques and understand principles! This article has a wealth of cases and videos with pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. Introduction to Linked List

1. Concept

A linear table can be said to be the most basic and simplest data structure, which represents a linear structure. The more common linear structures include arrays and linked lists .

The so-called linked list, as the name suggests, is a linked linear list, that is, a linked list is also a linear list. Unlike arrays, linked lists use chained storage, which is a non continuous and non-sequential memory space . Each independent element in the linked list is called a node, so the linked list is composed of a series of nodes.

The meaning of chained storage is as follows:

If we need to store a bunch of items, but there is not enough space to put all the items down at once, how can we not only put down all the items, but also find the location of all the items easily? We can try to adopt the following solution: When storing items, put a small note on the item every time you place an item, indicating where the next item is placed. In this way, we only need to remember the position of the first item, from the small note on the first item, we can find the second item, and then find the third item according to the content of the second item note. According to this method and so on, we can find all the items, which is the so-called chain storage.

2. Representation

Each node in the linked list consists of two parts: data field and pointer field . The data field is used to store the data content that the current node needs to store, and the pointer field is used to store the address of the next node of the current node. As shown below:

image.png

Figure 1 - Schematic diagram of the structure of the linked list

The details of the nodes shown in the figure above are as follows:

  • Next1 in the first node stores the memory address of the second node, so pointing an arrow to the second node can indicate the relationship between the two nodes.

  • There are no other nodes behind the last node, so there is no address content in the next5 pointer field of the last node, which can be represented by null in programming.

3. Features

Through the above, we can summarize the main characteristics of the linked list:

(1). From the perspective of the memory structure, the memory structure of the linked list is a discontinuous memory space , which is a data structure that connects a group of scattered memory blocks in series to store data;

(2). The linked list is composed of a series of nodes , each node includes two parts, one is the data field for storing data elements , and the other is the pointer field for storing the address of the next node . The logical order of the data elements in the linked list is realized through the address pointer;

(3). Compared with arrays, linked lists consume more memory space , because each node that stores data requires additional space to store address pointers.

2. Linked list classification

In work practice, there are mainly three types of linked lists that developers come into contact with: one-way linked list, doubly linked list, and circular linked list . Let me introduce you one by one.

1. Singly linked list

Each node of the singly linked list contains two parts, one part is the variable data storing the data, and the other part is the pointer next pointing to the next node. A singly linked list can only be read in one direction, and its structure is as follows:

image.png

Figure 2 - Schematic diagram of the structure of a one-way linked list

We take Java as an example to give the structure definition of a one-way linked list:

class Node{
    
    
	Object value;
    Node next;
}

2. Doubly linked list

A doubly linked list means that the linked list node is composed of three parts: the data field, the next node pointer field, and the previous node pointer field.

In the doubly linked list structure, you can start from the first node and find all nodes in turn according to the next node pointer field; similarly, you can also start from a specified node and find all nodes according to the previous node in the node Pointer address, forward to get the previous node in sequence. Specifically, the schematic diagram of the structure of the doubly linked list is as follows:

image.png

Figure 3 - Schematic diagram of the structure of the doubly linked list

As shown in FIG:

  • The first node is the first node of the entire linked list, and the content of the prev1 pointer of this node is null, indicating that there is no previous node.

  • The fifth node is the last node of the entire linked list, and the content of the next5 pointer is null, indicating that there is no next node in the future.

  • In addition, the middle three nodes, the next pointer and the prev pointer point to the next node and the previous node respectively, which can realize bidirectional search.

The node structure of the doubly linked list using Java is defined as follows:

class Node{
    
    
    Object value;
    Node next;
    Node prev;
}

3. Circular linked list

If we modify the next pointer field of the last node of the linked list from pointing to null to pointing to the first node, the whole linked list becomes a loop. Operate with a one-way linked list, as shown in the following figure:

Figure 4 - Schematic diagram of one-way circular linked list

As shown in the figure above, each node has two parts, the data field and the pointer field. This kind of circular linked list is called a one-way circular linked list . In the computer field, the one-way circular linked list is also called Joseph Loop (Josephu Loop), which is just for understanding. Of course, the doubly linked list can also be adjusted to a circular linked list, which is called a doubly linked list, as shown in the following figure:

image.png

Figure 5 - Schematic diagram of a two-way circular linked list

3. Storage principle

The storage method of arrays in memory is sequential storage (serial storage), and the storage method of linked lists in memory is random storage, as shown in the following figure:
insert image description here

Figure 6 - Schematic diagram of memory storage of linked list

Each node of the linked list is distributed in different locations of the memory and is associated by the next pointer. This can flexibly and effectively use the scattered fragment space. The first node of the linked list is called the head node. There is no next pointer of any node pointing to it, and its predecessor node is null. The head node is used to record the base address of the linked list. With it, you can traverse to get the data of the entire linked list. The last node of the linked list is called the tail node, and its next point is null.

4. Common operations of linked list

For the content of this article, we take the one-way linked list as an example to introduce the common operations of the linked list, mainly including: finding nodes, updating nodes, inserting nodes, and deleting nodes .

1. Find the node

When searching for elements, the linked list can only start from the head node and search backward one by one, as shown in the following figure:

Figure 7 - Schematic diagram of one-way linked list lookup nodes

Time complexity analysis, divided into two cases:

  • Find the head node: the head node is the first node of the linked list, and the result can be obtained directly, so the time complexity of finding the head node is O(1).

  • Find non-head nodes: If you look for non-head nodes, you need to search backwards from the head node to know the end of the entire linked list, so when looking for other nodes that are not head nodes, the time complexity is O(n), The time complexity is also O(n) in the worst case.

2. Update the node

Updating a node operation requires two steps:

  • Find the node to update;

  • Replace old data with new data.

As shown below:

image.png

Figure 8 - Schematic diagram of updating node data operation in singly linked list

Similar to the case of finding node operation time complexity, there are two cases of update time complexity:

  • Update the head node : the time complexity of updating the head node of the singly linked list is O(1);

  • Updating non-head nodes : The worst-case time complexity of updating other nodes is O(n).

3. Insert node

3.1 Tail insertion

Tail insertion is to point the next pointer of the last node to the newly inserted node, as shown in the following figure:

Figure 9 - Schematic diagram of inserting a node at the end of a one-way linked list

Time complexity analysis: As shown in the figure above, if a node is inserted at the end, it needs to be traversed from the beginning, so the time complexity of adding a tail node to a one-way linked list is O(n).

3.2 Head Insertion

Inserting a new node at the head requires two steps:

(1). Point the next pointer of the new node to the original head node;

(2). Make the new node the head node of the linked list.

As shown below:

Figure 10 - Schematic diagram of inserting a node at the head of a singly linked list

Time complexity analysis: Because the operation can be completed by directly pointing the pointer field of the new node to the head node, the time complexity of adding the head node is O(1).

3.3 Insertion in the middle

Inserting a node in the middle of the linked list also requires three steps:

(1). Search backward from the head node to find the position of the node to be inserted;

(2). The next pointer of the new node points to the node at the insertion position;

(3). The next pointer of the preceding node at the insertion position points to the new node;

The schematic diagram is as follows:

Figure 11 - Inserting a node in the middle of a singly linked list

Time complexity analysis: If you perform the operation of inserting a node, you first need to search backward from the head node to find the position to be inserted. Obviously, it is related to the size of the linked list, so the time complexity of inserting a node in the middle is O(n).

4. Delete node

4.1 Tail removal

If you want to delete the last node of the linked list, you only need to point the pointer field of the penultimate node to null, as shown in the following figure:

Figure 12 - Schematic diagram of deleting nodes at the end of a one-way linked list

Time complexity analysis: Because it is necessary to traverse from the beginning, the time complexity of deleting the tail node of the one-way linked list is O(n).

4.2 Head deletion

The head deletion is similar to the head insertion operation, just set the head node of the linked list to the next pointer of the original head node, as shown in the figure:

Figure 13 - Schematic diagram of deleting a node at the head of a one-way linked list

Time complexity analysis: The time complexity of deleting the head node is also O(1).

4.3 Intermediate deletion

The operation of deleting a node in the middle position is similar to the middle insertion operation, which requires three steps:

(1). Find the position of the node to be deleted from the head node backwards;

(2). Find the previous node and the next node of the deleted node;

(3). The next pointer of the preceding node to be deleted points to the next node of the element to be deleted;

As follows:

Figure 14 - Schematic diagram of deleting nodes in the middle of a singly linked list

Time complexity analysis: Because the search needs to be started from the head node, the time complexity is related to the size of the linked list, so the time complexity of deleting the intermediate node in the one-way linked list is O(n).


V. Conclusion

In this article, we learned the concept of linked list together, and got to know different linked list types such as one-way linked list, doubly linked list, and circular linked list. And taking the one-way linked list as an example, the hype of the nodes in the linked list and the corresponding time complexity analysis are analyzed. I wonder if you understand the linked list now?

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/131162555