Repository navigation
Expand file tree
/
Copy pathLinked list.cpp
More file actions
513 lines (468 loc) · 14.4 KB
/
Copy pathLinked list.cpp
File metadata and controls
513 lines (468 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// ============================================================
// 链表合集:单向链表 + 双向链表
// ============================================================
//
// 本文件涵盖:
// Part 1: 单向链表(Singly Linked List)—— 最简单的链表
// Part 2: 双向链表(Doubly Linked List)—— 带前驱指针
//
// ============================================================
#include<iostream>
using namespace std;
// 【阅读地图】
// 先读 SinglyLinkedList 的 insertHead / remove / reverse,再读双向链表。
// 核心不变量:head 为 nullptr,或指向第一个节点;从 head 沿 next 能恰好访问每个节点一次。
// 每次改链都遵守“先保存仍要访问的指针,再重接链接,最后释放节点”的顺序。
// ============================================================
// Part 1: 单向链表(Singly Linked List)
// ============================================================
//
// 【核心概念】
// 链表是最基础的非连续存储结构。每个节点只存数据 + 指向下一个的指针。
// 单向链表只能向后走,不能往回走。
//
// head → [data|next] → [data|next] → [data|next] → nullptr
//
// 【节点定义】
// data — 存储的数据(此处为 int)
// next — 指向下一个节点的指针,尾节点 next = nullptr
//
// 【关键操作的时间复杂度】
// 头插 O(1) — 改一下 new node 的 next 指向,再更新 head
// 尾插 O(n) — 需要先遍历到尾部
// 删除 O(n) — 需要知道前驱节点(单向链表没有 prev 指针)
// 查找 O(n) — 需要从头遍历
//
// 【单向 vs 双向】
// 单向链表省空间(少一个指针),但删除时必须维护 prev 指针来找到前驱。
// 双向链表多一个 prev 指针,但删除和反向遍历更方便。
//
// 【为什么需要链表?】
// 数组:插入/删除中间元素需要搬移后续所有元素 → O(n)
// 链表:插入/删除只需改指针 → O(1)(如果已知位置)
//
// 本质区别:数组是连续存储(随机访问快),链表是离散存储(插入删除快)。
//
// ============================================================
class SinglyNode
{
public:
int data;
SinglyNode* next;
SinglyNode(int d) : data(d), next(nullptr) {}
};
class SinglyLinkedList
{
private:
SinglyNode* head;
public:
SinglyLinkedList() : head(nullptr) {}
SinglyLinkedList(int n) { head = new SinglyNode(n); }
~SinglyLinkedList()
{
while (head)
{
SinglyNode* tmp = head;
head = head->next;
delete tmp;
}
}
// 头插法 O(1):新节点放到链表最前面
// 逆序插入 → 输出与插入顺序相反
// 例:依次头插 1,2,3 → 输出 3 2 1
void insertHead(int val)
{
SinglyNode* node = new SinglyNode(val);
node->next = head;
head = node;
}
// 尾插法 O(n):新节点放到链表末尾
// 顺序插入 → 输出与插入顺序一致
void insertTail(int val)
{
SinglyNode* node = new SinglyNode(val);
if (!head)
{
head = node;
return;
}
SinglyNode* cur = head;
while (cur->next)
cur = cur->next;
cur->next = node;
}
// 按值删除 O(n):删除第一个值为 val 的节点
// 关键:维护 prev 指针以修复链表
void remove(int val)
{
if (!head) { cout << "list is empty" << endl; return; }
// 删除的是头节点 → 直接推进 head
if (head->data == val)
{
SinglyNode* tmp = head;
head = head->next;
delete tmp;
return;
}
// 找前驱节点
SinglyNode* cur = head;
while (cur->next)
{
if (cur->next->data == val)
{
SinglyNode* tmp = cur->next;
cur->next = cur->next->next; // 跳过被删节点
delete tmp;
return;
}
cur = cur->next;
}
cout << "value not found" << endl;
}
// 查找 O(n)
bool search(int val)
{
SinglyNode* cur = head;
while (cur)
{
if (cur->data == val) return true;
cur = cur->next;
}
return false;
}
// 统计节点数
int count()
{
int n = 0;
SinglyNode* cur = head;
while (cur) { n++; cur = cur->next; }
return n;
}
// 反转链表(迭代法)
// 三个指针:prev/curr/next,遍历时逐个逆转 next 方向
// 例:1→2→3 → 3→2→1
void reverse()
{
SinglyNode *prev = nullptr, *curr = head, *next = nullptr;
while (curr)
{
next = curr->next; // 暂存后继
curr->next = prev; // 反转指向
prev = curr; // prev 前进
curr = next; // curr 前进
}
head = prev; // 原尾节点变新头
}
void display()
{
SinglyNode* cur = head;
while (cur)
{
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
};
// ============================================================
// Part 2: 双向链表(Doubly Linked List)
// ============================================================
//
// 【核心概念】
// 每个节点有两个指针:prev(前驱)和 next(后继)。
// 相比单向链表,多了一倍的指针开销,但换来了以下好处:
// - 可以在 O(1) 时间删除任意节点(已知节点指针时)
// - 可以双向遍历
// - 删除操作不需要额外的前驱指针遍历
//
// 【头节点 first】
// first 指向链表的第一个节点,first->prev 始终为 nullptr。
// first == nullptr 表示空链表。
//
// 【支持的操作】
// Count() — 统计节点个数,O(n)
// AscendingInsert — 升序插入,O(n)
// DescendingInsert — 降序插入,O(n)
// Insert — 尾插法,O(n)(可通过维护尾指针优化到 O(1))
// Delete — 按值删除第一个匹配节点,O(n)
// Search — 按值查找,O(n)
// Display — 正向遍历打印,O(n)
//
// 【与单向链表的比较】
// 单向链表:Delete 需要额外的前驱指针 + 从头遍历
// 双向链表:Delete 直接用 current->prev 即可找到前驱
//
// AscendingInsert/DescendingInsert 利用了双向链表的 prev 指针,
// 在找到插入位置后可以方便地在当前节点之前插入。
//
// ============================================================
class Node
{
public:
int data;
Node* next;
Node* prev;
Node(int d) : data(d), next(nullptr), prev(nullptr) {}
};
class Linkedlist
{
private:
Node* first; // 头节点指针,空链表时为 nullptr
public:
Linkedlist() : first(nullptr) {}
// 创建一个包含初始值的链表
Linkedlist(int n) { first = new Node(n); }
// 析构:遍历释放所有节点
~Linkedlist()
{
Node* current = first;
while (current)
{
Node* tmp = current;
current = current->next;
delete tmp;
}
}
// 统计链表中的节点个数
int Count()
{
int result = 0;
Node* current = first;
while (current)
{
result++;
current = current->next;
}
return result;
}
// 升序插入:遍历找到第一个比 val 大的节点,插在它前面
// 如果所有节点都 ≤ val,则插入到末尾
void AscendingInsert(int val)
{
Node* newNode = new Node(val);
if (!first)
{
first = newNode;
return;
}
Node* current = first;
while (current)
{
if (current->data <= val)
{
if (current->next)
{
current = current->next; // 继续找更大的
}
else
{
// 已到链表尾部,val 是当前最大值,插入末尾
current->next = newNode;
newNode->prev = current;
return;
}
}
else
{
// current->data > val,在 current 之前插入
Node* pre = current->prev;
newNode->next = current;
newNode->prev = pre;
current->prev = newNode;
if (pre)
{
pre->next = newNode;
}
else
{
first = newNode; // 插入到头节点之前,更新 first
}
return;
}
}
}
// 降序插入:遍历找到第一个比 val 小的节点,插在它前面
// 逻辑与升序插入对称,只是比较方向相反
void DescendingInsert(int val)
{
Node* newNode = new Node(val);
if (!first)
{
first = newNode;
return;
}
Node* current = first;
while (current)
{
if (current->data >= val)
{
if (current->next)
{
current = current->next;
}
else
{
current->next = newNode;
newNode->prev = current;
return;
}
}
else
{
Node* pre = current->prev;
newNode->next = current;
newNode->prev = pre;
current->prev = newNode;
if (pre)
{
pre->next = newNode;
}
else
{
first = newNode;
}
return;
}
}
}
// 尾插法:遍历到链表末尾,在尾部插入新节点
// 时间复杂度 O(n),可维护 tail 指针优化到 O(1)
void Insert(int val)
{
Node* newnode = new Node(val);
if (!first)
{
first = newnode;
return;
}
Node* current = first;
while (current)
{
if (current->next)
{
current = current->next;
}
else
{
current->next = newnode;
newnode->prev = current;
break;
}
}
}
// 按值删除:找到第一个值为 val 的节点并删除
void Delete(int val)
{
if (!first)
{
cout << "list is empty" << endl;
return;
}
Node* current = first;
while (current)
{
if (current->data == val)
{
// 调整前驱的 next 指针
if (current->prev)
{
current->prev->next = current->next;
}
else
{
first = current->next; // 删除的是头节点
}
// 调整后继的 prev 指针
if (current->next)
{
current->next->prev = current->prev;
}
delete current;
return;
}
current = current->next;
}
cout << "value not found" << endl;
}
// 查找值为 val 的节点是否存在
bool Search(int val)
{
Node* current = first;
while (current)
{
if (current->data == val)
{
return true;
}
current = current->next;
}
return false;
}
// 正向遍历打印
void Display()
{
Node* current = first;
while (current)
{
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
// ============================================================
// 测 试 主 函 数
// ============================================================
int main()
{
cout << "╔══════════════════════════════════╗" << endl;
cout << "║ 链 表 综 合 测 试 ║" << endl;
cout << "╚══════════════════════════════════╝" << endl;
// ==================== Part 1: 单向链表 ====================
cout << "\n===== Part 1: 单向链表 =====" << endl;
SinglyLinkedList sl;
cout << "\n头插 1,2,3 (逆序): ";
sl.insertHead(1);
sl.insertHead(2);
sl.insertHead(3);
sl.display(); // 3 2 1
cout << "节点数=" << sl.count() << " (expect 3)" << endl;
cout << "尾插 4,5: ";
sl.insertTail(4);
sl.insertTail(5);
sl.display(); // 3 2 1 4 5
cout << "search(1)=" << sl.search(1) << " (expect 1)" << endl;
cout << "search(9)=" << sl.search(9) << " (expect 0)" << endl;
cout << "反转: ";
sl.reverse();
sl.display(); // 5 4 1 2 3
cout << "删除4: ";
sl.remove(4);
sl.display(); // 5 1 2 3
cout << "删除头5: ";
sl.remove(5);
sl.display(); // 1 2 3
// ==================== Part 2: 双向链表 ====================
cout << "\n===== Part 2: 双向链表 =====" << endl;
Linkedlist ll;
cout << "\n升序插入 5,2,8,1,3: ";
ll.AscendingInsert(5);
ll.AscendingInsert(2);
ll.AscendingInsert(8);
ll.AscendingInsert(1);
ll.AscendingInsert(3);
ll.Display(); // 1 2 3 5 8
cout << "节点数=" << ll.Count() << " (expect 5)" << endl;
cout << "search(3)=" << ll.Search(3) << " (expect 1)" << endl;
cout << "search(9)=" << ll.Search(9) << " (expect 0)" << endl;
cout << "删除1(头): "; ll.Delete(1); ll.Display();
cout << "删除8(尾): "; ll.Delete(8); ll.Display();
cout << "删除3(中): "; ll.Delete(3); ll.Display();
cout << "节点数=" << ll.Count() << " (expect 2)" << endl;
cout << "降序插入10,6: ";
ll.DescendingInsert(10);
ll.DescendingInsert(6);
ll.Display(); // 10 6 5 2
cout << "\n所有测试完成!" << endl;
return 0;
}