The beauty of data structures and algorithms (linked list)

1. What is a linked list

  • . Like an array, a linked list is a linear list.
  • In terms of memory structure, the memory structure of a 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.
  • Each memory block in the linked list is called a Node . In addition to storing data , the node also needs to record the address of the next node on the chain , that is, the successor pointer next .

2. Why use linked list?

  • Inserting and deleting data is highly efficient at O(1)** level ( just change the pointer to point ), and random access is inefficient O(n) level ( requires traversal from the head of the chain to the tail of the chain ).
  • Compared with arrays, the memory space consumption is larger, because each node storing data needs additional space to store the successor pointer.

3. Commonly used linked list

1. Singly linked list

1) Each node contains only one pointer, the successor pointer .
2) A singly linked list has two special nodes, the head node and the tail node .
The address of the first node represents the entire linked list, and the successor pointer of the tail node points to the empty address null.
3) Performance characteristics: The time complexity of inserting and deleting nodes is O(1) , and the time complexity of searching is O(n) .
insert image description here
insert image description here

2. Circular linked list

1) It is consistent with the singly linked list except that the successor pointer of the tail node points to the address of the head node.
2) It is suitable for storing data with cyclic characteristics, such as the Joseph problem .
insert image description here

3. Doubly linked list

1) In addition to storing data, the node also has two pointers that point to the previous node address (precursor pointer prev) and the next node address (successor pointer next).
2) The predecessor pointer prev of the first node and the successor pointer of the tail node both point to an empty address.
3) Performance characteristics: Compared with a singly linked list, storing the same data requires more storage space. Insertion and deletion operations are more O(1) efficient than singly linked lists.
insert image description here

4. Doubly Circular Linked List

The predecessor pointer of the head node points to the tail node, and the successor pointer of the tail node points to the head node.
insert image description here

Fourth, the difference between array and linked list

1. Time complexity of insertion, deletion and random access

Array: The time complexity of insertion and deletion is O(n), and the time complexity of random access is O(1).
Linked list: The time complexity of insertion and deletion is O(1), and the time complexity of random access is O(n).

2. Array disadvantages

1) If the application memory space is large, such as 100M, but if there is no 100M continuous space in the memory space, the application will fail, although the available memory space exceeds 100M.
2) The size is fixed. If the storage space is insufficient, the capacity needs to be expanded. Once the capacity is expanded, data replication must be performed, which is very time-consuming.

3. Disadvantages of linked list

1) The memory space consumption is larger because additional space is required to store pointer information .
2) Frequent insertion and deletion of linked lists will lead to frequent memory application and release, which may easily cause memory fragmentation. In the case of Java language, it may also cause frequent GC (automatic garbage collector) operations.

5. How to implement LRU buffer elimination strategy with linked list and array respectively?

1) What is cache?

Caching is a technology to improve data reading performance, and it is widely used in hardware design and software development, such as common CPU cache, database cache, browser cache and so on.

2) Why use cache?

The size of the cache is limited. When the cache is full, which data should be cleared out and which data should be kept? Cache elimination strategy is required.

3) What is the cache elimination strategy?

Refers to the priority of cleaning data when the cache is full.

4) What are the cache elimination strategies?

The three common ones include FIFO (First In, First Out) , LFU (Least Frenquently Used) , and LRU (Least Recently Used) .

5) Linked list implements LRU cache elimination strategy

When the accessed data is not stored in the cached linked list, the data is directly inserted into the linked list header, and the time complexity is O(1); when the accessed data exists in the stored linked list, the node corresponding to the data is inserted into To the head of the linked list, the time complexity is O(n). If the cache is full, the data at the end of the linked list will be cleaned up, and the time complexity is O(1).

6) Array implements LRU cache elimination strategy

Method 1: Save the latest access data at the first position, and clean up the last position first

When the accessed data does not exist in the cached array, the data is directly inserted into the first element of the array. At this time, all elements of the array need to be moved backward by one position, and the time complexity is O(n); when the accessed data is When it exists in the cached array, the data is found and inserted into the first position of the array. At this time, the array elements need to be moved, and the time complexity is O(n). When the cache is full, the data at the end is cleaned up, and the time complexity is O(1).

Method 2: The first position is cleaned first, and the last position saves the latest access data

When the accessed data does not exist in the cached array, the data is directly added to the array as the most current element. The time complexity is O(1); when the accessed data exists in the cached array, the data is found and Insert it into the position of the last element of the current array. At this time, the array element needs to be moved, and the time complexity is O(n). When the cache is full, the element at the first position of the array is cleaned up, and the remaining array elements need to be moved forward by one place as a whole, and the time complexity is O(n). (Optimization: When cleaning, you can consider cleaning a certain amount at one time, thereby reducing the number of cleanings and improving performance.)

6. How to easily write correct code

1. Understand the meaning of a pointer or reference

What is a pointer? A pointer is a variable that stores the addresses of other variables. Assigning an ordinary variable to a pointer variable is actually assigning its address to the pointer variable.

2. Beware of lost pointers and memory leaks

When inserting and deleting nodes, pay attention to holding the following nodes before operating, otherwise, once the predecessor pointers of the following nodes are disconnected, they cannot be accessed again, resulting in memory leaks.

3. Use Sentinels to simplify the difficulty

Insertion and deletion of linked lists require special handling for inserting the first node and deleting the last node. The sentinel object can be used to judge without boundaries. The sentinel object of the linked list is the head node that only stores the pointer but not the data.
insert image description here

4. Focus on boundary condition processing

When operating the linked list, consider the situation that the linked list is empty, one node, two nodes, the head node, and the tail node. Learning data structures and algorithms is mainly about mastering a series of ideas, and can also develop the habit of considering boundaries in other coding.

5. Draw an example to help you think

For more complex operations, you can draw with a pen and paper to free up your brain capacity for logical processing (time-for-space thinking), which is also convenient for inspection after completion.
insert image description here

6. Write more and practice more, there are no shortcuts

Whatever makes perfect, no matter what algorithm it is, only after repeated practice can it be done at your fingertips. (again nonsense)

Guess you like

Origin blog.csdn.net/qq_54729417/article/details/122871680