5. Linked list (on)

Application : LRU cache elimination algorithm

The linked list is also a linear list .

The memory structure of a linked list is a discontinuous memory space . A group of discontinuous memory spaces are connected in series to form a data structure for data storage.

 

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 linked list, that is, the subsequent pointer next.

 

Linked list features

1. Insert, delete data efficiency O (1) level (only need to change the pointer).

Random access efficiency O (n) level. (Need to traverse from the head of the list to the end of the chain)

 

2. Compared with the array, the memory space consumption is greater, because each node that stores data requires additional space to store subsequent pointers.

 

Commonly used linked lists: single linked list, circular linked list, double linked list

1. Single linked list:

(1) Each node contains only one pointer, the successor pointer.

(2) There are two special nodes, the first node and the tail node. The address of the linked list is represented by the address of the first node, and the successor pointer of the tail node is NULL.

(3) Performance characteristics: insertion and deletion time complexity O (1), search time complexity O (n).

 

2. Circular linked list

(1) Except that the successor pointer of the tail node points to the first node, it is consistent with the singly linked list.

(2) It is suitable for storing data with cyclic characteristics, such as the Joseph problem.

 

3. Doubly linked list

(1) In addition to storing data, there are two pointers to the previous node address and the next node address.

(2) The precursor pointer of the first node and the successor pointer of the tail node both point to NULL.

(3) Compared with singly linked lists, storing the same data requires more storage space. Insert and delete operations are more efficient than singly linked lists.

 

Comparison of arrays and linked lists

1. Array disadvantages

(1) If you apply for a large memory space, such as 100M, but the memory space does not have 100M of continuous space, the application fails, even if the memory available space is greater than 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 is required, which is very time-consuming.

2. Disadvantages of linked lists

(1) The memory space consumption is greater, because additional space is needed to store pointer information.

(2) Frequent insert and delete operations on the linked list will lead to frequent memory application and release, which is easy to cause memory fragmentation.

 

cpu cache mechanism

The array is simple and easy to use. The implementation uses continuous memory space. You can use the CPU's cache mechanism to pre-read the data in the group, so the access efficiency is higher. The linked list is not continuously stored in memory, so it is not friendly to the CPU cache and there is no way to effectively read ahead.

When the CPU reads data from the memory , it will first load the read data into the CPU's cache . Each time the CPU reads data from memory, it does not only read the specific address to be accessed, but reads a data block (I am not sure about this size ...) and save it in the CPU cache, and then access the memory next time When the data is found, it will start from the CPU cache . If it is found, it does not need to be fetched from the memory . In this way, a mechanism that is faster than the memory access speed is realized, which is the meaning of the existence of the CPU cache: introduced to make up for the difference between the memory access speed is too slow and the CPU execution speed is fast .

For arrays , the storage space is continuous , so when loading a subscript, you can also load several subscript elements into the CPU cache so that the execution speed will be faster than the linked list storage with discontinuous storage space.

 

Linked list writing skills

1. Understand the meaning of pointers or references

Assigning a variable to a pointer actually assigns the address of the variable to the pointer, or conversely, the pointer stores the memory address of the variable, points to the variable, and the variable can be found through the pointer.

 

p-> next = q. This line of code says that the next pointer in node p stores the memory address of node q.

p-> next = p-> next-> next. This line of code indicates that the next pointer of the p node stores the memory address of the next node of the p node.

 

Variables store values, and each variable has an address.

The value of the pointer is the address of a variable.

Using pointers, you can indirectly read or update the value of a variable without knowing the variable name.

 

The way of go language:

Declare the variable var x int expression & x (the address of x) to get a pointer to an integer variable whose type is an integer pointer (* int).

If the value of the pointer is called p, we say that p points to x, or p contains the address of x.

The variable pointed to by p is written as * p, and the expression * p indicates that the value of the variable is obtained as an integer.

* p represents a variable, which can appear on the left side of the assignment operator and is used to update the value of the variable.

 

x := 1

p: = & x // p is a pointer to the address of variable x

fmt.Println (* p) // * p, get the value of the variable pointed to by the pointer p, that is, the value of x

* p = 2 // that is, x = 2, change the value of the variable x through the pointer p

fmt.Println (x) // Output 2

 

The members of the structure or the elements of the array are variables.

The zero value of the pointer type is nil, test p! = Nil, the result is true, indicating that p points to a variable.

The two pointers are currently equal only if they point to the same variable or both are nil.

 

2. Beware of pointer loss and memory leaks

For example, the operation sequence of inserting a new node in the linked list

 

 

x->next = p->next

p->next = x

 

 

3. Use Sentinel to simplify the difficulty of realization

 

4. Focus on handling boundary conditions

If the linked list is empty, will the code work properly?

If the linked list contains only one node, can the code work properly?

If the linked list contains only two nodes, can the code work properly?

Does the code logic work properly when dealing with head and tail nodes?

 

5. Learn to draw

Draw a flow chart, draw logic on the diagram

 

6. Write more and practice more

Singly linked list reversal

Detection of rings in linked list

Two ordered linked lists merge

Delete the nth last node of the linked list

Find the middle node of the linked list

 

 

 

Published 127 original articles · Likes 24 · Visits 130,000+

Guess you like

Origin blog.csdn.net/Linzhongyilisha/article/details/99348299