当前位置:网站首页>Basic knowledge of linked list
Basic knowledge of linked list
2022-07-07 22:49:00 【Qingshan's green shirt】
Basic knowledge of linked list
Type of linked list
What is a linked list , A linked list is a linear structure connected in series by pointers , Each node is made up of two parts , One is the data field and the other is the pointer field ( Holds a pointer to the next node ), The pointer field of the last node points to null( A null pointer means ).
The entry point of the link is called the head node of the list, which is head.

Type of linked list
Single chain list
Just now
Double linked list
A node in a single linked list can only point to the next node of a node .
Double linked list : Each node has two pointer fields , One points to the next node , One points to the previous node .
Double linked list You can query forward or backward .

Circular linked list
Circular linked list , seeing the name of a thing one thinks of its function , The linked list is connected end to end .
Circular linked list can be used to solve Joseph ring problem .

storage
Arrays are continuously distributed in memory , But linked lists are not continuously distributed in memory .
The linked list links the nodes in the memory through the pointer of the pointer field .
So the nodes in the linked list are not continuously distributed in memory , It's scattered on some address in memory , The allocation mechanism depends on the memory management of the operating system .
As shown in the figure, the starting node of this linked list is 2, The termination node is 7, Each node is distributed in a different memory address space , Connected in series by a pointer .

Code —— Definition of linked list
// Single chain list
struct ListNode{
int val;// Data stored on the node
ListNode *next;// Pointer to the next node
ListNode(int x):val(x),next(NULL){
}// Constructors
};
It's OK without this constructor ,c++ A constructor will be generated by default .
But this constructor does not initialize any member changes , Two examples :
Initialize the node by defining its own constructor :
ListNode* head = new ListNode(5);
Use the default constructor to initialize the node
ListNode* head = new ListNode(); head->val = 5;
So if you don't define a constructor and use the default constructor , You can't assign values to variables directly during initialization !
Operation of linked list
Delete node

As long as C Node next The pointer Point to E Node is OK .
That's what some students said ,D Nodes are still in memory ? It's just not in this list .
That's true , So in C++ It's better to release this manually D node , Free up this memory .(free)
Other languages such as Java、Python, It has its own memory recovery mechanism , You don't have to release it yourself .
Add node
It can be seen that the addition and deletion of linked list are O(1) operation , And it doesn't affect other nodes .
But be careful , If you delete the fifth node , You need to find the fourth node from the beginning through next Pointer to delete , The time complexity of the search is O(n).
Performance analysis

Note the insertion of the linked list / The deletion time complexity is O(1) Because the previous node is known , If you simply delete a specific element from a linked list , So you need to traverse + Delete , Time complexity is O(n) 了 .
When an array is defined , The length is fixed , If you want to change the length of the array , You need to redefine a new array .
The length of the linked list can be unfixed , And it can be added and deleted dynamically , Suitable for data volume is not fixed , Add and delete frequently , Less query scenarios .
边栏推荐
- ASP.NET Core入门五
- Blender exchange group, welcome to the water group ~
- How pyGame rotates pictures
- Build an "immune" barrier in the cloud to prepare your data
- Write in front -- Talking about program development
- Aspose. Word operation word document (I)
- 6-3 find the table length of the linked table
- Yarn开启ACL用户认证之后无法查看Yarn历史任务日志解决办法
- C # Development -- pit encountered in JS intermodulation
- php 获取图片信息的方法
猜你喜欢

Unity FAQ (I) lack of references

Yarn cannot view the historical task log of yarn after enabling ACL user authentication. Solution

应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设

How to choose the appropriate automated testing tools?

0-5vac to 4-20mA AC current isolated transmitter / conversion module

How to judge whether the input content is "number"

Ni9185 and ni9234 hardware settings in Ni Max

行测-图形推理-7-相异图形类
Redis official ORM framework is more elegant than redistemplate

ASP. Net core introduction V
随机推荐
php 记录完整对接腾讯云直播以及im直播群聊 所遇到的坑
Customer case | China law network, through observing the cloud, greatly shortens the time of fault location
[azure microservice service fabric] start the performance monitor in the SF node and set the method of capturing the process
Visual studio 2019 installation
【Azure微服务 Service Fabric 】因证书过期导致Service Fabric集群挂掉(升级无法完成,节点不可用)
php 获取图片信息的方法
Debezium系列之:支持 mysql8 的 set role 语句
Antd date component appears in English
OpeGL personal notes - lights
Apple further entered the financial sector through the 'virtual card' security function in IOS 16
Form组件常用校验规则-2(持续更新中~)
Matplotlib quick start
应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
[problem] pytorch installation
The free styling service of Dyson's official direct store is now open for appointment. Pioneer Technology interprets the styling concept of hair care and helps consumers unlock diversified and shiny s
PKPM 2020软件安装包下载及安装教程
[azure microservice service fabric] the service fabric cluster hangs up because the certificate expires (the upgrade cannot be completed, and the node is unavailable)
Debezium系列之:mysql墓碑事件
Remember aximp once Use of exe tool
Revit secondary development - project file to family file