转自:http://www.cnblogs.com/wwang/archive/2010/11/28/1889281.html
链表是C语言编程中常用的数据结构,比如我们要建一个整数链表,一般可能这么定义:
1
2
3
4
|
struct
int_node {
int
val;
struct
int_node *next;
};
|
为了实现链表的插入、删除、遍历等功能,另外要再实现一系列函数,比如:
1
2
3
4
5
6
7
8
9
10
11
|
void
insert_node(
struct
int_node **head,
int
val);
void
delete_node(
struct
int_node *head,
struct
int_node *current);
void
access_node(
struct
int_node *head)
{
struct
int_node *node;
for
(node = head; node != NULL; node = node->next) {
// do something here
}
}
|
如果我们的代码里只有这么一个数据结构的话,这样做当然没有问题,但是当代码的规模足够大,需要管理很多种链表,难道需要为每一种链表都要实现一套插入、删除、遍历等功能函数吗?
熟悉C++的同学可能会说,我们可以用标准模板库啊,但是,我们这里谈的是C,在C语言里有没有比较好的方法呢?
Mr.Dave在他的博客里介绍了自己的实现,这个实现是个很好的方案,各位不妨可以参考一下。在本文中,我们把目光投向当今开源界最大的C项目--Linux Kernel,看看Linux内核如何解决这个问题。
Linux内核中一般使用双向链表,声明为struct list_head,这个结构体是在include/linux/types.h中定义的,链表的访问是以宏或者内联函数的形式在include/linux/list.h中定义。
1
2
3
|
struct
list_head {
struct
list_head *next, *prev;
};
|
Linux内核为链表提供了一致的访问接口。
1
2
3
4
5
|
void
INIT_LIST_HEAD(
struct
list_head *list);
void
list_add(
struct
list_head *
new
,
struct
list_head *head);
void
list_add_tail(
struct
list_head *
new
,
struct
list_head *head);
void
list_del(
struct
list_head *entry);
int
list_empty(
const
struct
list_head *head);
|
以上只是从Linux内核里摘选的几个常用接口,更多的定义请参考Linux内核源代码。
我们先通过一个简单的实作来对Linux内核如何处理链表建立一个感性的认识。
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
|
#include <stdio.h>
#include "list.h"
struct
int_node {
int
val;
struct
list_head list;
};
int
main()
{
struct
list_head head, *plist;
struct
int_node a, b;
a.val = 2;
b.val = 3;
INIT_LIST_HEAD(&head);
list_add(&a.list, &head);
list_add(&b.list, &head);
list_for_each(plist, &head) {
struct
int_node *node = list_entry(plist,
struct
int_node, list);
printf
(
"val = %d\n"
, node->val);
}
return
0;
}
|
看完这个实作,是不是觉得在C代码里管理一个链表也很简单呢?
代码中包含的头文件list.h是我从Linux内核里抽取出来并做了一点修改的链表处理代码,现附在这里给大家参考,使用的时候只要把这个头文件包含到自己的工程里即可。

