Chinaunix首页 | 论坛 | 博客
  • 博客访问: 900883
  • 博文数量: 453
  • 博客积分: 7865
  • 博客等级: 少将
  • 技术积分: 5673
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:21
个人简介

时光荏苒..

文章分类
文章存档

2015年(46)

2014年(22)

2013年(68)

2012年(218)

2011年(99)

分类: LINUX

2012-01-11 10:51:31

双向循环链表
struct list_head{
内核中很多地方使用双向循环链表来维护一些信息,比如 任务队列。
双向循环链表定义于include/linux/list.h,只有两个成员next与prev分别指向后继与前趋结点。一般 内嵌到别的结构体内使用。避免了每个需要双向循环链表的数据结构都自己维护指针并编写链表操作函数
struct list_head {
    struct list_head *next, *prev;    
};

一些常用的相关宏和函数如下:
INIT_LIST_HEAD(struct list_head *list(简单写作ptr)) 初始化ptr节点为表头,将前趋与后继都指向自己。
{
 list->next = list;
 list->prev = list;
LIST_HEAD(name) 声明并初始化双向循环链表name
 
static inline void __list_add(struct list_head *new,
         struct list_head *prev,
         struct list_head *next)    插入 new   prev next     prev的next要改变,同时new的prev
                                                             next的prev要改变,同时new的next
{
 next->prev = new;
 new->next = next;  改变值  相关所有指针值
 new->prev = prev;
 prev->next = new;
}
向链表中在prev与next之间插入元素new

static inline void list_add(struct list_head *new, struct list_head *head)
在链表中头节点后插入元素new,调用__list_add()实现。
 
 
 

static inline void list_add_tail(struct list_head *new, struct list_head *head)
在链表末尾插入元素new,调用__list_add()实现。
__list_add(new, head->prev, head);  插入末尾,new它的下一个是head   上一个是head的上一个。
 
 
 

static inline void __list_del(struct list_head * prev, struct list_head * next)
删除链表中prev与next之间的元素。
static inline void list_del(struct list_head *entry)
删除链表中的元素entry。

static inline void list_del_init(struct list_head *entry)
从链表中删除元素entry,并将其初始化为新的链表。
static inline void list_move(struct list_head *list, struct list_head *head)
从链表中删除list元素,并将其加入head链表。
static inline void list_move_tail(struct list_head *list, struct list_head *head)
把list移动到链表末尾。

static inline int list_empty(const struct list_head *head)
测试链表是否为空。

static inline void __list_splice(struct list_head *list, struct list_head *head)
将链表list与head合并。
static inline void list_splice(struct list_head *list, struct list_head *head)
在list不为空的情况下,调用__list_splice()实现list与head的合并。
static inline void list_splice_init(struct list_head *list, struct list_head *head)
将两链表合并,并将list初始化。

list_entry(ptr, type, member)

list_for_each(pos, head),__list_for_each(pos, head)
列出链表head中的每一项,前者有调用prefetch进行预取而后者没有。。
list_for_each_prev(pos, head)
反向列出链表head中的每一项,有预取。
 
-------------------------------------
 
 
 
 
 
 
阅读(478) | 评论(0) | 转发(0) |
0

上一篇:PM linux电源管理 英文文档

下一篇:脚气

给主人留下些什么吧!~~