# 双链表

双链表大概是软件中最为常见也是最简单的数据结构之一了。所以在内核中的定义和使用也不难，只是用法上可能和其他地方的实现略有不同。

## 单个的样子

这个样子很普通，一共两个成员，各自指向前后就行了。

```
    list_head
    +-----------------------+
    |prev                   |
    |next                   |
    |    (struct list_head*)|
    +-----------------------+
```

## 组合时的样子

组合起来样子也相对比较简单。

```
    list_head          
    +-----------+
    |prev   next|<-+
    +-----------+  |
       ^           |
       |           |
       |           |      Parent Data              Parent Data
       |           |      +-----------------+      +-----------------+
       |           +----->|list_head        |<---->|list_head        |<-----+
       |                  +-----------------+      +-----------------+      |
       |                                                                    |
       +--------------------------------------------------------------------+
```

这里想要强调的一点是，内核中通常把list\_head结构嵌入到某个真实的数据结构中，然后由一个list\_head结构作为链表头。

## 常用的API

常用的增删类API有：

* list\_add()
* list\_add\_tail()
* list\_del()
* list\_move()
* list\_move\_tail()

常用的遍历类API有：

* list\_for\_each()
* list\_for\_each\_prev()
* list\_for\_each\_entry()
* list\_for\_each\_entry\_reverse()


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://richardweiyang-2.gitbook.io/kernel-exploring/00-index-3/01-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
