玩转C链表

简介:
作者:  wwang  来源:  博客园  发布时间: 2010-12-05 15:23  阅读: 217 次   原文链接    全屏阅读   [收藏]  

  链表是C语言编程中常用的数据结构,比如我们要建一个整数链表,一般可能这么定义:

1 struct int_node {
2         int val;
3         struct int_node *next;
4 };

  为了实现链表的插入、删除、遍历等功能,另外要再实现一系列函数,比如:

1 void insert_node(struct int_node *head, struct int_node *current);
2 void delete_node(struct int_node *head, struct int_node *current);
3 void access_node(struct int_node *head)
4 {
5         struct int_node *node;
6         for (node = head; node != NULL; node = node->next) {
7                 // do something here
8         }
9 }

  如果我们的代码里只有这么一个数据结构的话,这样做当然没有问题,但是当代码的规模足够大,需要管理很多种链表,难道需要为每一种链表都要实现一套插入、删除、遍历等功能函数吗?熟悉C++的同学可能会说,我们可以用标准模板库啊,但是,我们这里谈的是C,在C语言里有没有比较好的方法呢?

  Mr.Dave在他的博客里介绍了自己的实现,这个实现是个很好的方案,各位不妨可以参考一下。在本文中,我们把目光投向当今开源界最大的C项目--Linux Kernel,看看Linux内核如何解决这个问题。

  Linux内核中一般使用双向链表,声明为struct list_head,这个结构体是在include/linux/types.h中定义的,链表的访问是以宏或者内联函数的形式在include/linux/list.h中定义。

1 struct list_head {
2     struct list_head *next, *prev;
3 };

  Linux内核为链表提供了一致的访问接口。

1 void INIT_LIST_HEAD(struct list_head *list);
2 void list_add(struct list_head *newstruct list_head *head);
3 void list_add_tail(struct list_head *newstruct list_head *head);
4 void list_del(struct list_head *entry);
5 int list_empty(const struct list_head *head);

   以上只是从Linux内核里摘选的几个常用接口,更多的定义请参考Linux内核源代码。我们先通过一个简单的实作来对Linux内核如何处理链表建立一个感性的认识。

01 #include <stdio.h>
02 #include "list.h"
03 struct int_node {
04         int val;
05         struct list_head list;
06 };
07 int main()
08 {
09         struct list_head head, *plist;
10         struct int_node a, b;
11         a.val = 2;
12         b.val = 3;
13         INIT_LIST_HEAD(&head);
14         list_add(&a.list, &head);
15         list_add(&b.list, &head);
16         list_for_each(plist, &head) {
17                 struct int_node *node = list_entry(plist, struct int_node, list);
18                 printf("val = %d\n", node->val);
19         }
20         return 0;
21 }

  看完这个实作,是不是觉得在C代码里管理一个链表也很简单呢?代码中包含的头文件list.h是我从Linux内核里抽取出来并做了一点修改的链表处理代码,现附在这里给大家参考,使用的时候只要把这个头文件包含到自己的工程里即可。

复制代码
#ifndef __C_LIST_H
#define  __C_LIST_H

typedef unsigned 
char  u8;
typedef unsigned 
short  u16;
typedef unsigned 
int  u32;
typedef unsigned 
long  size_t;

#define  offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

/* *
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define  container_of(ptr, type, member) (type *)((char *)ptr -offsetof(type,member))

/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define  LIST_POISON1 ((void *) 0x00100100)
#define  LIST_POISON2 ((void *) 0x00200200)

struct  list_head {
struct  list_head  * next,  * prev;
};

/* *
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define  list_entry(ptr, type, member) \
container_of(ptr, type, member)


#define  LIST_HEAD_INIT(name) { &(name), &(name) }

#define  LIST_HEAD(name) \
struct  list_head name  =  LIST_HEAD_INIT(name)

static  inline  void  INIT_LIST_HEAD( struct  list_head  * list)
{
list
-> next  =  list;
list
-> prev  =  list;
}

/* *
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define  list_for_each(pos, head) \
for  (pos  =  (head) -> next; pos  !=  (head); pos  =  pos -> next)

/* *
* list_for_each_r - iterate over a list reversely
* @pos: the &struct list_head to use as a loop counter.
* @head: the head for your list.
*/
#define  list_for_each_r(pos, head) \
for  (pos  =  (head) -> prev; pos  !=  (head); pos  =  pos -> prev) 

/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static  inline  void  __list_add( struct  list_head  * new ,
struct  list_head  * prev,
struct  list_head  * next)
{
next
-> prev  =   new ;
new -> next  =  next;
new -> prev  =  prev;
prev
-> next  =   new ;
}

/* *
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static  inline  void  list_add( struct  list_head  * new struct  list_head  * head)
{
__list_add(
new , head, head -> next);
}

/* *
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static  inline  void  list_add_tail( struct  list_head  * new struct  list_head  * head)
{
__list_add(
new , head -> prev, head);
}

/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static  inline  void  __list_del( struct  list_head  *  prev,  struct  list_head  *  next)
{
next
-> prev  =  prev;
prev
-> next  =  next;
}

/* *
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
static  inline  void  list_del( struct  list_head  * entry)
{
__list_del(entry
-> prev, entry -> next);
entry
-> next  =  LIST_POISON1;
entry
-> prev  =  LIST_POISON2;
}


/* *
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static  inline  int  list_empty( const   struct  list_head  * head)
{
return  head -> next  ==  head;
}


static  inline  void  __list_splice( struct  list_head  * list,
struct  list_head  * head)
{
struct  list_head  * first  =  list -> next;
struct  list_head  * last  =  list -> prev;
struct  list_head  * at  =  head -> next;

first
-> prev  =  head;
head
-> next  =  first;

last
-> next  =  at;
at
-> prev  =  last;
}

/* *
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static  inline  void  list_splice( struct  list_head  * list,  struct  list_head  * head)
{
if  ( ! list_empty(list))
__list_splice(list, head);
}


#endif   //  __C_LIST_H
复制代码

  list_head通常是嵌在数据结构内使用,在上文的实作中我们还是以整数链表为例,int_node的定义如下:

1 struct int_node {
2         int val;
3         struct list_head list;
4 };

  使用list_head组织的链表的结构如下图所示:

  遍历链表是用宏list_for_each来完成。

1 #define list_for_each(pos, head) \
2     for (pos = (head)->next; prefetch(pos->next), pos != (head); \
3             pos = pos->next)

  在这里,pos和head均是struct list_head。在遍历的过程中如果需要访问节点,可以用list_entry来取得这个节点的基址。

1 #define list_entry(ptr, type, member) \
2     container_of(ptr, type, member)

  我们来看看container_of是如何实现的。如下图所示,我们已经知道TYPE结构中MEMBER的地址,如果要得到这个结构体的地址,只需要知道MEMBER在结构体中的偏移量就可以了。如何得到这个偏移量地址呢?这里用到C语言的一个小技巧,我们不妨把结构体投影到地址为0的地方,那么成员的绝对地址就是偏移量。得到偏移量之后,再根据ptr指针指向的地址,就可以很容易的计算出结构体的地址。

  list_entry就是通过上面的方法从ptr指针得到我们需要的type结构体。

  Linux内核代码博大精深,陈莉君老师曾把它形容为“覆压三百余里,隔离天日”(摘自《阿房宫赋》),可见其内容之丰富、结构之庞杂。内核里有着众多重要的数据结构,具有相关性的数据结构之间很多都是用本文介绍的链表组织在一起,看来list_head结构虽小,作用可真不小。

  Linux内核是个伟大的工程,其源代码里还有很多精妙之处,值得C/C++程序员认真去阅读,即使我们不去做内核相关的工作,阅读精彩的代码对程序员自我修养的提高也是大有裨益的。

分类:  linux C lauguage


本文转自wanqi博客园博客,原文链接:http://www.cnblogs.com/wanqieddy/archive/2011/09/10/2173219.html 如需转载请自行联系原作者
相关文章
|
10月前
|
数据挖掘 测试技术 项目管理
2025年测试用例管理看这一篇就够了 ----Codes 开源免费、全面的测试管理解决方案
Codes 是国内首款重新定义 SaaS 模式的开源项目管理平台,支持云端认证、本地部署、全部功能开放,并且对 30 人以下团队免费。它通过整合迭代、看板、度量和自动化等功能,简化测试协同工作,使敏捷测试更易于实施。并提供低成本的敏捷测试解决方案,如同步在线离线测试用例、流程化管理缺陷、低代码接口自动化测试和 CI/CD,以及基于迭代的测试管理和测试用时的成本计算等,践行敏捷测试。
2025年测试用例管理看这一篇就够了 ----Codes 开源免费、全面的测试管理解决方案
|
机器学习/深度学习 数据可视化 Python
【Python实战】——神经网络识别手写数字(二)
【Python实战】——神经网络识别手写数字(三)
|
关系型数据库 MySQL 网络安全
记一次mysql 5.7.34编译过程错误
记一次mysql 5.7.34编译过程错误
292 0
|
XML 设计模式 JSON
Spring MVC 深度解析与应用实践(2)
4.2 校验 Spring MVC 提供了基于 JSR 303 和 JSR 349 的校验功能。我们可以在 Model 对象的属性上使用 JSR 303 和 JSR 349 提供的注解,如 @NotNull、@Size、@Pattern 等,来定义校验规则。然后,在 Controller 方法中使用 @Valid 注解来触发校验:
227 0
|
消息中间件 存储 JSON
关于RabbitMQ
MQ是一种应用程序键一步通讯的技术,MQ是消息队列的缩写(Message Queue) 在MQ中,消息由一个应用程序发送到一个称为队列的中间件中,接着被中间件存储,并最终被另一个或多个消费者应用程序读取和处理; MQ组成:消息——生产者——队列——中间件——消费者!
209 0
|
应用服务中间件 Linux 网络安全
linux安装tomcat
linux安装tomcat
|
运维 自然语言处理 监控
serverless 案例 | 游戏 心动网络(TapTap)
serverless 案例 | 游戏 心动网络(TapTap)
332 0
serverless 案例 | 游戏 心动网络(TapTap)
|
缓存 前端开发 API
优酷播放体验优化实战(七)--优酷高性能弹幕渲染技术大揭秘
优酷高性能弹幕渲染引擎的目标,是在全平台实现对弹幕内容的高效渲染。渲染的内容包括文本、emoji、普通图片、apng动图和3D mesh等元素,并且支持节奏弹幕、燃弹幕、弹幕穿人、流光弹幕等各种特效玩法。下面,将对优酷高性能弹幕渲染引擎所涉及的技术做一次大揭秘。
1909 0
优酷播放体验优化实战(七)--优酷高性能弹幕渲染技术大揭秘
|
SQL 关系型数据库 MySQL
|
Ubuntu
Ubuntu find 命令
Ubuntu find 命令
210 0