Chinaunix首页 | 论坛 | 博客
  • 博客访问: 215057
  • 博文数量: 27
  • 博客积分: 527
  • 博客等级: 中士
  • 技术积分: 262
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-07 19:52
文章分类
文章存档

2013年(6)

2012年(21)

分类: C/C++

2012-11-20 23:07:30

list_head用法经常在Linux的kernel里面经常看见,所以记录用法以来备忘
(1)list.h文件在include/linux/list.h

点击(此处)折叠或打开

  1. #ifndef _LIST_H
  2. #define _LIST_H

  3. #define _INLINE_ static inline

  4. struct list_head {
  5.     struct list_head *next, *prev;
  6. };

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

  8. #define LIST_HEAD(name) \ //定义并初始化头结点head
  9.     struct list_head name = LIST_HEAD_INIT(name)

  10. #define INIT_LIST_HEAD(ptr) do {\             //初始化头结点ptr,因此需要首先定义ptr
  11.     (ptr)->next = (ptr); (ptr)->prev = (ptr); \
  12. } while (0)

  13. _INLINE_ void __list_add(struct list_head *add,
  14.         struct list_head *prev,
  15.         struct list_head *next)
  16. {
  17.     next->prev = add;
  18.     add->next = next;
  19.     add->prev = prev;
  20.     prev->next = add;
  21. }

  22. _INLINE_ void list_add(struct list_head *add, struct list_head *head)//每次添加节点到head之后,始终都是添加到头结点之后
  23. {
  24.     __list_add(add, head, head->next);
  25. }

  26. _INLINE_ void list_add_tail(struct list_head *add, struct list_head *head)//每次添加节点都是头结点之前,由于是循环链表,就是说添加到链表尾部
  27. {
  28.     __list_add(add, head->prev, head);
  29. }

  30. _INLINE_ void __list_del(struct list_head *prev, struct list_head *next)
  31. {
  32.     next->prev = prev;
  33.     prev->next = next;
  34. }

  35. _INLINE_ void list_del(struct list_head *entry)//删除节点
  36. {
  37.     __list_del(entry->prev, entry->next);
  38. }

  39. _INLINE_ void list_del_init(struct list_head *entry)
  40. //删除节点,并初始化被删除的结点(也就是使被删除的结点的prev和next都指向自己)
  41. {
  42.     __list_del(entry->prev, entry->next);
  43.     INIT_LIST_HEAD(entry);
  44. }

  45. _INLINE_ int list_empty(struct list_head *head)//判断链表是否为空
  46. {
  47.     return head->next == head;
  48. }

  49. _INLINE_ void list_splice(struct list_head *list, struct list_head *head)
  50. //通过两个链表的head,进行连接
  51. {
  52.     struct list_head *first = list->next;

  53.     if (first != list) {
  54.         struct list_head *last = list->prev;
  55.         struct list_head *at = head->next;

  56.         first->prev = head;
  57.         head->next = first;

  58.         last->next = at;
  59.         at->prev = last;
  60.     }
  61. }

  62. #define list_entry(ptr, type, member) \
  63.     ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

  64. #define list_for_each(pos, head) \                                        
  65. //遍历链表,此时删除节点的操作可能会出错
  66.     for (pos = (head)->next; pos != (head); pos = pos->next) //新代码中出现prefetch() 可以不考虑,用于预取以提高遍历速度

  67. #define list_for_each_safe(pos, pnext, head) \      //遍历链表,可以同时有删除节点的操作
  68.     for (pos = (head)->next, pnext = pos->next; pos != (head); \
  69.             pos = pnext, pnext = pos->next)

  70. #undef _INLINE_
  71. #endif
(2)写一个测试程序,利用各个函数实现功能

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include "list.h"

  4. struct int_node
  5. {
  6.     int val;
  7.     struct list_head list;
  8. };

  9. int main()
  10. {
  11.     struct list_head head,*plist;
  12.     struct int_node a,b,c;
  13.     
  14.     a.val = 1;
  15.     b.val = 2;
  16.     c.val = 3;
  17.     
  18.     INIT_LIST_HEAD(&head);            //初始化链表头
  19.     list_add_tail(&a.list,&head);     //添加节点
  20.     list_add_tail(&b.list,&head);
  21.     list_add_tail(&c.list,&head);

  22.     list_for_each(plist,&head)//遍历链表,打印结果
  23.     {
  24.         struct int_node *node = list_entry(plist,struct int_node,list);//然后取得数据项,因此一般来说和list_for_each配合使用
  25.         printf("val = %d\n",node->val);
  26.     }//print 1 2 3

  27.     printf("*******************************************\n");
  28.     list_del_init(&b.list);            //删除节点b
  29.     list_for_each(plist,&head)         //重新遍历链表,打印结果
  30.     {
  31.         struct int_node *node = list_entry(plist,struct int_node,list);
  32.         printf("val = %d\n",node->val);
  33.     }//print 1 3

  34.     printf("*******************************************\n");
  35.     struct int_node d,e;
  36.     struct list_head head1;
  37.     d.val = 4;
  38.     e.val = 5;
  39.     INIT_LIST_HEAD(&head1);            //重新建立链表,表头为head1
  40.     list_add_tail(&d.list,&head1);
  41.     list_add_tail(&e.list,&head1);

  42.     list_splice(&head1,&head);        //把两个链表进行连接
  43.     list_for_each(plist,&head)
  44.     {
  45.         struct int_node *node = list_entry(plist,struct int_node,list);
  46.         printf("val = %d\n",node->val);
  47.     }//print 4 5 1 3

  48.     printf("*******************************************\n");
  49.     if(!list_empty(&head))          //判断链表是否为空
  50.     {
  51.         printf("the list is not empty!\n");
  52.         
  53.     }

  54.     return 0;
  55. }

输出结果如下:
val = 1
val = 2
val = 3
*******************************************
val = 1
val = 3
*******************************************
val = 4
val = 5
val = 1
val = 3
*******************************************
the list is not empty!

(3)list_for_each()与list_for_each_safe()
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next)
由定义可知,list_del(pos)(将pos的前后指针指向undefined state)panic,list_del_init(pos)(将pos前后指针指向自身)导致死循环。

#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)

由定义可知,safe函数首先将pos的后指针缓存到n,处理一个流程后再赋回pos,避免了这种情况的发生。
因此只遍历链表不删除节点时可以使用前者,若有删除节点的操作,则要使用后者。

由safe的说明可知,是专门为删除节点时准备的:iterate over a list safe against removal of list entry。其他带safe的处理也基本源于这个原因。

参考资料:

http://blog.csdn.net/king_208/article/details/5524175
http://www.cnblogs.com/wwang/archive/2010/11/28/1889281.html




阅读(2220) | 评论(1) | 转发(0) |
0

上一篇:extern “C”

下一篇:list_head用法(一)

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

xiaoqiang_linux2015-11-26 17:51:32

list head 是双向循环链表