惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
T
Tailwind CSS Blog
Microsoft Azure Blog
Microsoft Azure Blog
爱范儿
爱范儿
P
Proofpoint News Feed
博客园_首页
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
AI
AI
云风的 BLOG
云风的 BLOG
有赞技术团队
有赞技术团队
SecWiki News
SecWiki News
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
V
V2EX
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
大猫的无限游戏
大猫的无限游戏
G
GRAHAM CLULEY
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
博客园 - 司徒正美
V
Visual Studio Blog
L
Lohrmann on Cybersecurity
C
Cisco Blogs
A
About on SuperTechFans
N
News | PayPal Newsroom
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
罗磊的独立博客
A
Arctic Wolf
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Secure Thoughts
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
T
Tenable Blog
Engineering at Meta
Engineering at Meta

云藉のBlog

随笔·一种名为爱好的坎 近况·丙午立夏 近况·丙午惊蛰 近况·乙巳立春 2026 近况·乙巳冬至 近况·乙巳重阳 在自己的服务器上部署Waline 近况·乙巳秋分 近况·乙巳·夏 四年 十八 青岛六十七中校歌 近况·6月9日 二〇二五 近况·甲辰·夏 三周年 十七·启航 近况·甲辰·春
数据结构·单链表
云藉 · 2025-09-22 · via 云藉のBlog

数据结构·单链表

链表由节点构成,单链表中的节点包含着数据和next指针,写成结构体的形式就是

1
2
3
4
5
typedef int ElemType;
typedef struct _Node {
ElemType data;
struct _Node *next;
} Node;

初始化链表

1
2
3
4
5
6
Node *InitList() {
Node *L = (Node *)malloc(sizeof(Node));
L->data = 0;
L->next = NULL;
return L;
}

插入

插入的方法分为:头插、尾插、任意插。

头插法

注意:一定是先让目标节点P的next指向后继节点,再让前驱节点的next指向P,不然后继节点的地址会被覆盖掉。

1
2
3
4
5
6
void HeadInsert(Node *L, ElemType e) {
Node *P = (Node *)malloc(sizeof(Node));
P->data = e;
P->next = L->next;
L->next = P;
}

尾插法

1
2
3
4
5
6
7
8
9
10
void *TailInsert(Node *L, ElemType e) {
Node *P = L, *Tail = (Node *)malloc(sizeof(Node));
while (P->next) {
P = P->next;
}

P->next = Tail;
Tail->data = e;
Tail->next = NULL;
}

任意位置插入

1
2
3
4
5
6
7
8
9
void Insert(Node *L, int index, ElemType e) {
Node *P = L, *Q = (Node *)malloc(sizeof(Node));
for (int i = 0; i < index - 1; i++) {
P = P->next;
}
Q->next = P->next;
P->next = Q;
Q->data = e;
}

遍历

1
2
3
4
5
6
7
8
void Traverse(Node *L) {
Node *P = L->next;
while (P!=NULL) {
printf("%c", P->data);
P = P->next;
}
printf("\n");
}

删除

1
2
3
4
5
6
7
8
9
void Delete(Node *L, int index) {
Node *P = L;
for (int i = 0; i < index - 1; i++) {
P = P->next;
}
Node *Q = P->next;
P->next = Q->next;
free(Q);
}

销毁

1
2
3
4
5
6
7
8
void Destory(Node *L) {
Node *P = L, *Q;
while (P) {
Q = P->next;
free(P);
P = Q;
}
}

查找

1
2
3
4
5
6
7
8
9
10
int Find(Node *L, ElemType e) {
Node *P = L;
int index = 0;
while (P) {
P = P->next;
index++;
if (P->data == e) return index;
}
return -1;
}