# 双向链表

双向链表是一种链式数据结构，其中每个元素（或称节点）保存一个值以及两个链接：一个指向序列中的下一个节点，一个指向前一个节点。[美国国家标准与技术研究院](https://www.edgepedia.org/zh/national-institute-of-standards-and-technology)的《算法与数据结构词典》将其定义为“一种链表变体，其中每一项既有一个指向后一项的链接，也有一个指向前一项的链接”，这使得既可以向前访问也可以向后访问，并且一旦已知某一项的位置，就能在常数时间内删除任意项。<sup>[1](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)</sup>它也被称为双路链表或对称链表。<sup>[1](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 节点结构 | 每个节点存储一个值、一个 next 指针和一个 prev 指针；在线性表中，头节点的 prev 指针和尾节点的 next 指针为空<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup> |
| 删除 | 删除任意已知地址的节点需要常数时间，因为该节点已经知道自己的前驱<sup>[1](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)</sup> |
| 按位置访问 | 对于含 n 个元素的表，返回第 i 个元素需要 O(n) 时间，因为节点不能按索引寻址<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup> |
| 标准库 | Java 的 `LinkedList`（自 Java 1.2 起）、C++ 的 `std::list` 和 Rust 的 `std::collections::LinkedList` 都是双向链表的实现<sup>[3](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/LinkedList.html)</sup><sup> • </sup><sup>[4](https://en.cppreference.com/cpp/container/list)</sup><sup> • </sup><sup>[5](https://doc.rust-lang.org/stable/std/collections/struct.LinkedList.html)</sup> |
| 内核使用 | Linux 内核的双向链表是循环的，并将一个 `struct list_head` 成员嵌入被包含的结构体中<sup>[6](https://www.kernel.org/doc/html/v6.17/core-api/list.html)</sup> |
| 典型应用 | LRU 缓存将哈希映射与双向链表配对，以实现 O(1) 的查找、提升和淘汰<sup>[7](https://www.geeksforgeeks.org/dsa/lru-cache-implementation-using-double-linked-lists/)</sup> |

## 结构与基本操作

每个节点携带三个字段：存储的值、指向后一元素的 next 指针、指向前一元素的 prev 指针。<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup>在线性表中，头节点的 prev 指针和尾节点的 next 指针为空，这两个空值标志着结构的边界。<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup>与单向链表相比，双向链表每个节点多一个字段；单向链表实现更简单，数据字段只有双向链表的三分之二，但遍历时无法回溯。<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup>

**插入与删除。** 在两个已有节点之间插入一个节点意味着重新连接四个指针：新节点的 next 和 prev、前驱的 next、后继的 prev。删除则相反，而且由于节点已经持有指向前驱的指针，删除一个已知节点需要常数时间，这正是 NIST 定义所强调的性质。<sup>[1](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)</sup>单向链表则需要先找到前驱，这需要从头遍历。

**哨兵节点。** 许多实现添加两个哑节点——一个 prev 引用为空的头哨兵和一个 next 引用为空的尾哨兵——它们不存储数据。有了哨兵，就没有真实节点持有空链接，空表就是两个哨兵互相指向，因此插入和删除代码不需要为两端做特殊处理。<sup>[8](https://ebooks.inflibnet.ac.in/csp01/chapter/linked-list-implementation-of-list-adt/)</sup>一种相关的设计是循环双向链表，其中第一个节点的 prev 链接指向最后一个节点，最后一个节点的 next 链接指向第一个节点。<sup>[9](https://cse.mait.ac.in/pdf/LAB%20MANUAL/DS.pdf)</sup>

**搜索。** 按位置访问是弱点：对于含 n 个元素的表，返回第 i 个元素需要 O(n) 时间，因此在 m 次迭代的循环中执行 ElementAt 操作需要 O(n<sup>2</sup>) 的工作量。<sup>[2](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)</sup>在有序双向链表上进行二分查找仍然需要 O(n) 次遍历，与线性查找相同，但只进行 O(log n) 次比较，当比较代价高昂时这可以使其有效。<sup>[1](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)</sup>

## 变体

实践中反复出现三种结构选择。线性表在两端以空指针终止。循环双向链表将两端连接起来，使遍历可以从任意节点开始并无限循环，这适合轮转调度和循环操作。<sup>[9](https://cse.mait.ac.in/pdf/LAB%20MANUAL/DS.pdf)</sup><sup> • </sup><sup>[10](https://www.geeksforgeeks.org/dsa/applications-of-linked-list-data-structure/)</sup>侵入式链表将链接字段嵌入被链接的对象内部，而不是将它们包装在单独的节点对象中。[Linux 内核](https://www.edgepedia.org/zh/linux-kernel)采用这种方法：一个应存在于链表中的结构体声明一个 `struct list_head` 成员，链表代码通过 `container_of()` 模式访问有效载荷，因此一个通用链表实现可以服务于所有被包含的类型。<sup>[6](https://www.kernel.org/doc/html/v6.17/core-api/list.html)</sup>内核的链表也是循环的，因此从头节点到达尾节点是向后一步，从尾节点到达头节点是向前一步越过末尾。<sup>[6](https://www.kernel.org/doc/html/v6.17/core-api/list.html)</sup>

## 标准库实现

Java 的 `java.util.LinkedList` 是 `List` 和 `Deque` 接口的双向链表实现，自 Java 1.2 起可用。它允许所有元素包括空值，索引操作从更接近请求索引的一端开始遍历。该类不是同步的，其迭代器是快速失败的，当链表在迭代器之外被结构性修改时，会尽最大努力抛出 `ConcurrentModificationException`。<sup>[3](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/LinkedList.html)</sup>

C++ 的 `std::list` 支持在容器中任意位置常数时间插入和移除元素，不支持快速随机访问，通常实现为双向链表。与单向链式容器 `std::forward_list` 相比，它提供双向迭代，但空间效率较低。添加、移除或移动元素不会使迭代器或引用失效；只有当对应元素被删除时，迭代器才会失效。<sup>[4](https://en.cppreference.com/cpp/container/list)</sup>

Rust 的 `std::collections::LinkedList` 是一个拥有节点所有权的双向链表，允许在两端以常数时间压入和弹出元素。文档中包含一个明确的警告：选择 `Vec` 或 `VecDeque` 几乎总是更可取的选择，因为基于数组的容器往往运行更快、内存使用更高效，并且能更好地利用 [CPU 缓存](https://www.edgepedia.org/zh/cpu-cache)。<sup>[5](https://doc.rust-lang.org/stable/std/collections/struct.LinkedList.html)</sup>

## 应用

双向链表的区别性特征是对已知地址的节点进行廉价的移除和重排。这一特征正是 LRU（最近最少使用）缓存所需要的：哈希映射在常数时间内定位键对应的节点，链表维护最近使用顺序，因此将一项提升到前端和淘汰后端的一项都是常数时间的指针操作。<sup>[7](https://www.geeksforgeeks.org/dsa/lru-cache-implementation-using-double-linked-lists/)</sup>同样的哈希映射加链表的设计也出现在[操作系统](https://www.edgepedia.org/zh/operating-system)页面置换中。<sup>[11](https://www.chabrovs.tech/media/articles/OS/pdf/Operating_systems_OSes.pdf)</sup>

其他有记载的用途遵循同样的模式。操作系统线程调度器维护可运行进程的双向链表，使调度器能够高效地向前和向后遍历。<sup>[12](https://stackoverflow.com/questions/40661716/real-life-use-of-doubly-linked-list)</sup>撤销和重做栈、浏览器前进和后退按钮、最近使用列表以及游戏对象列表都依赖于在项与其邻居之间双向移动。<sup>[10](https://www.geeksforgeeks.org/dsa/applications-of-linked-list-data-structure/)</sup>在 Linux 内核中，双向链表遍布源代码树，从内存块记账到网络端口列表，内核文档将侵入式循环链表视为链接内核对象的标准方式。<sup>[6](https://www.kernel.org/doc/html/v6.17/core-api/list.html)</sup><sup> • </sup><sup>[13](https://github.com/torvalds/linux/blob/master/include/linux/memory.h)</sup>

## 性能权衡

**内存。** 一个基本节点是三个指针：数据、向前和向后。

**缓存局部性。** 由于节点是单独分配的，它们的地址实际上是分散的，沿着 next 指针访问可能会错过 CPU 缓存。Linux 内核文档直言不讳地指出，在简单数组就足够的情况下，链表很少是最佳结构，因为其数据局部性差。<sup>[6](https://www.kernel.org/doc/html/v6.17/core-api/list.html)</sup>一项将链表与基于数组的替代方案进行比较的实测发现，链表的缓存未命中次数多 3.4 倍。<sup>[15](https://github.com/djiangtw/data-structures-in-practice-public/blob/main/manuscript/chapters/chapter05.md)</sup>这种影响取决于布局：一个 Java `LinkedList` 如果其节点恰好在内存中连续放置，读取随机元素的速度比节点故意分散在堆上的情况快三倍，而一个不断添加和移除元素的应用程序通常处于分散的情况。<sup>[16](https://dev.java/learn/api/collections-and-streams/collections-framework/arraylist-vs-linkedlist/)</sup>

**迭代陷阱。** 按索引迭代 Java `LinkedList` 是一种 O(n<sup>2</sup>) 模式，因为每次索引访问都从较近的一端重新开始遍历；一个 1000 元素的链表按索引迭代大约需要半毫秒，而使用迭代器只需 4 微秒。<sup>[16](https://dev.java/learn/api/collections-and-streams/collections-framework/arraylist-vs-linkedlist/)</sup>

**并发。** 在多线程代码中，用互斥锁串行化所有操作一直是维护共享双向链表的事实上的通行方法；针对异步共享内存系统存在高度并发和非阻塞双向链表的研究实现，其中移动操作读取共享内存而不改变结构的状态。<sup>[17](https://arxiv.org/abs/1112.1141)</sup><sup> • </sup><sup>[18](https://arxiv.org/abs/1408.1935)</sup>

## 历史

链表信息结构由[艾伦·纽厄尔](https://www.edgepedia.org/zh/allen-newell)（Allen Newell）、约翰·克利福德·肖（John Clifford Shaw）和[赫伯特·A·西蒙](https://www.edgepedia.org/zh/herbert-a-simon)（Herbert A. Simon）开发，是他们逻辑理论机（Logic Theory Machine）的核心元素，该程序是为[兰德公司](https://www.edgepedia.org/zh/rand-corporation)（RAND Corporation）的 JOHNNIAC 计算机编写的；西蒙确定 1955 年 12 月 15 日为逻辑理论家首次纸上模拟的日期，编程于 1956 年完成。<sup>[19](https://www.journals.uchicago.edu/doi/full/10.1086/683527)</sup><sup> • </sup><sup>[20](https://escholarship.org/content/qt88q1j6z3/qt88q1j6z3_noSplash_6bcb1b3cd41a7a6eb009ac813283a35f.pdf)</sup>他们 1957 年的报告《逻辑理论机的编程》列出了链表结构必须满足的要求，包括在计算过程中随意创建新链表、在链表前端和后端添加元素，以及让同一项同时出现在任意数量的链表上。<sup>[21](https://iiif.library.cmu.edu/file/Newell_box00049_fld04210_doc0002/Newell_box00049_fld04210_doc0002.pdf)</sup>链表常被错误地归功于纽厄尔及其同事后来的 IPL 语言工作，那是在 1956 年。<sup>[22](https://cs.stackexchange.com/questions/108958/the-origin-of-list-data-structure)</sup>

## References

1. [Paul E. Black, "doubly linked list", Dictionary of Algorithms and Data Structures, NIST, entry modified 19 July 2021](https://xlinux.nist.gov/dads/HTML/doublyLinkedList.html)
2. [Data Structures and Algorithms, Class Notes Section 1, University of Florida](https://www.cise.ufl.edu/~mssz/DatStrucAlg/DSAintro.html)
3. [LinkedList (Java SE 24 & JDK 24), Oracle documentation](https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/util/LinkedList.html)
4. [std::list, cppreference.com](https://en.cppreference.com/cpp/container/list)
5. [LinkedList in std::collections, Rust standard library documentation](https://doc.rust-lang.org/stable/std/collections/struct.LinkedList.html)
6. [Linked Lists in Linux, The Linux Kernel documentation](https://www.kernel.org/doc/html/v6.17/core-api/list.html)
7. [LRU Cache Implementation using Doubly Linked List, GeeksforGeeks](https://www.geeksforgeeks.org/dsa/lru-cache-implementation-using-double-linked-lists/)
8. [Linked List Implementation of List ADT, INFLIBNET e-books](https://ebooks.inflibnet.ac.in/csp01/chapter/linked-list-implementation-of-list-adt/)
9. [Data Structures Lab Manual, Maharaja Agrasen Institute of Technology](https://cse.mait.ac.in/pdf/LAB%20MANUAL/DS.pdf)
10. [Applications of linked list data structure, GeeksforGeeks](https://www.geeksforgeeks.org/dsa/applications-of-linked-list-data-structure/)
11. [Operating systems (OSes), course notes](https://www.chabrovs.tech/media/articles/OS/pdf/Operating_systems_OSes.pdf)
12. [Real life use of doubly linked list, Stack Overflow](https://stackoverflow.com/questions/40661716/real-life-use-of-doubly-linked-list)
13. [linux/include/linux/memory.h, torvalds/linux on GitHub](https://github.com/torvalds/linux/blob/master/include/linux/memory.h)
14. [Doubly Linked List Implementation: Two Pointers Are Better Than One, SpaceComplexity](https://solvelogs.com/learn/doubly-linked-lists/)
15. [Data Structures in Practice, chapter 5, GitHub](https://github.com/djiangtw/data-structures-in-practice-public/blob/main/manuscript/chapters/chapter05.md)
16. [Choosing the Right Implementation Between ArrayList and LinkedList, Dev.java](https://dev.java/learn/api/collections-and-streams/collections-framework/arraylist-vs-linkedlist/)
17. [Highly-Concurrent Doubly-Linked Lists, arXiv:1112.1141](https://arxiv.org/abs/1112.1141)
18. [Non-Blocking Doubly-Linked Lists with Good Amortized Complexity, arXiv:1408.1935](https://arxiv.org/abs/1408.1935)
19. [Of Models and Machines: Implementing Bounded Rationality, Isis Vol. 106 No. 3](https://www.journals.uchicago.edu/doi/full/10.1086/683527)
20. [Building the Second Mind: 1956, eScholarship](https://escholarship.org/content/qt88q1j6z3/qt88q1j6z3_noSplash_6bcb1b3cd41a7a6eb009ac813283a35f.pdf)
21. [Newell and Shaw, Programming the Logic Theory Machine, RAND/CMU archive](https://iiif.library.cmu.edu/file/Newell_box00049_fld04210_doc0002/Newell_box00049_fld04210_doc0002.pdf)
22. [The origin of list data structure, Computer Science Stack Exchange](https://cs.stackexchange.com/questions/108958/the-origin-of-list-data-structure)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Data structures › Lists and linked structures

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/doubly-linked-list

English original: https://www.edgepedia.org/doubly-linked-list
