手机版
你好,游客 登录 注册
背景:
阅读新闻

嵌入式C语言学习笔记

Linux中霸道的双向链表源码与应用

[日期:2012-06-11] 来源:Linux社区  作者:PirateLeo [字体:大 中 小]

C语言作为面向过程的语言,想写出灵活的结构与封装需要很高技巧。

但由于C语言的高效,几乎所有操作系统和面向对象语言的最底层实现都使用了C语言。即,使用C完成面向对象的封装。

这次通过整理与仿写Linux的双向链表让我体会到了一些C语言封装的核心技巧。

这个双向链表的巧妙之处在于1)利用宏将“函数”入参扩展出了“结构类型”; 2)利用纯地址偏移获取结构体指针;

下面是具体实现:链表的实现由于是宏定义,都在libList.h中。

测试程序testList.c与testList.h。

libList.h

  1. #ifndef _LIBLIST_H   
  2. #define _LIBLIST_H   
  3.   
  4. #ifdef __cplusplus   
  5. extern "C" {  
  6. #endif   
  7.   
  8. /* 链表头节点定义 */  
  9. typedef struct tagstList_Head {  
  10.     struct tagstList_Head *next, *prev;  
  11. }stList_Head;  
  12.   
  13. /* 初始化双向链表 */  
  14. #define list_init(head) do          \   
  15. {                       \  
  16.     (head)->next = (head)->prev = (head); \  
  17. } while(0)  
  18.   
  19. /* 在指定元素(where)之后插入新元素(item) */  
  20. #define list_add(item, towhere) do  \   
  21. {                   \  
  22.     (item)->next = (towhere)->next;   \  
  23.     (item)->prev = (towhere);    \  
  24.     (towhere)->next = (item);    \  
  25.     (item)->next->prev = (item);  \  
  26. } while(0)  
  27.   
  28. /* 在指定元素(where)之前插入新元素(item) */  
  29. #define list_add_before(item, towhere)  \   
  30.     list_add(item,(towhere)->prev)  
  31.   
  32. /* 删除某个元素 */  
  33. #define list_remove(item) do            \   
  34. {                       \  
  35.     (item)->prev->next = (item)->next; \  
  36.     (item)->next->prev = (item)->prev; \  
  37. } while(0)  
  38.   
  39. /* 正向遍历链表中所有元素 */  
  40. #define list_for_each_item(item, head)\   
  41.     for ((item) = (head)->next; (item) != (head); (item) = (item)->next)  
  42.   
  43. /* 反向遍历链表中所有元素 */  
  44. #define list_for_each_item_rev(item, head) \   
  45.     for ((item) = (head)->prev; (item) != (head); (item) = (item)->prev)  
  46.   
  47. /* 根据本节点(item)获取节点所在结构体(type)的地址 */  
  48. /* 节点item地址(member的地址) - 该链表元素member在结构体中的偏移 */  
  49. #define list_entry(item, type, member) \   
  50.     ((type *)((char *)item - (char *)(&((type *)0)->member)))  
  51.   
  52. /* 判断链表是否为空 */  
  53. #define list_is_empty(head) \   
  54.     ((head)->next == (head))  
  55.   
  56. /* 获取指定位置上一元素 */  
  57. #define list_prev_item(item)\   
  58.     ((head)->prev)  
  59.   
  60. #ifdef __cplusplus   
  61. }  
  62. #endif   
  63.   
  64. #endif  

testList.h

  1. #ifndef _TESTLIST_H   
  2. #define _TESTLIST_H   
  3.   
  4. #ifdef __cplusplus   
  5. extern "C" {  
  6. #endif   
  7.   
  8. #include "libList.h"   
  9.   
  10. typedef struct tagInteger  
  11. {  
  12.     int idx;  
  13.     stList_Head stListHead;  
  14. }stInteger;  
  15.   
  16. extern void testlist();  
  17.   
  18. #ifdef __cplusplus   
  19. }  
  20. #endif   
  21.   
  22. #endif  

这种双向链表的特点在于:在需要链表管理的结构中定义一个链表节点变量,然后所有的操作都是针对这个链表节点进行的。在需要获取该结构时使用list_entry即可。

testList.c

  1. #ifdef __cplusplus   
  2. extern "C" {  
  3. #endif   
  4.   
  5. #include <malloc.h>   
  6. #include <stddef.h>   
  7. #include "stdafx.h"   
  8. #include "testList.h"   
  9.   
  10. void testlist()  
  11. {  
  12.     int i = 0;  
  13.     stList_Head     stListHead;  
  14.     stList_Head     *pstListHead= NULL;  
  15.   
  16.     stInteger       *pstInteger = NULL;  
  17.     stList_Head     *pstTmpList = NULL;  
  18.   
  19.     /* 链表初始化 */  
  20.     pstListHead = &stListHead;  
  21.     list_init(pstListHead);  
  22.   
  23.     /* 生成节点并挂入链表 */  
  24.     for(i = 0; i < 32; i++)  
  25.     {  
  26.         pstInteger = (stInteger *)malloc(sizeof(stInteger));  
  27.         if (NULL == pstInteger)  
  28.         {  
  29.             printf("pstInteger malloc fail!");  
  30.             return;  
  31.         }  
  32.         pstInteger->idx = 100 - i;  
  33.   
  34.         /* 挂到头结点之前 */  
  35.         list_add_before(&pstInteger->stListHead, pstListHead);  
  36.   
  37.         /* 挂到头结点之后 */  
  38.         //list_add(&pstInteger->stListHead, pstListHead);   
  39.     }  
  40.       
  41.     /* 正向遍历链表中每一个元素并输出 */  
  42.     list_for_each_item(pstTmpList, pstListHead)  
  43.     {  
  44.         if(NULL != pstTmpList)  
  45.         {  
  46.             pstInteger = list_entry(pstTmpList, stInteger, stListHead);  
  47.             printf("%d ",pstInteger->idx);  
  48.         }  
  49.     }  
  50.       
  51.     return;  
  52.   
  53. }  
  54.   
  55. #ifdef __cplusplus   
  56. {  
  57. #endif  
linux
【内容导航】
第1页:几个重要关键字 第2页:栈与队列的实现
第3页:Linux中霸道的双向链表源码与应用
相关资讯       C语言 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款