Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1437852
  • 博文数量: 704
  • 博客积分: 10140
  • 博客等级: 上将
  • 技术积分: 6230
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-15 20:41
文章分类

全部博文(704)

文章存档

2013年(1)

2012年(16)

2011年(536)

2010年(151)

分类:

2011-11-07 22:11:18

http://vincent.blog.chinaunix.net
今天有空,就把一个C函数的链表封装一下,附件放到最后:

文件:listx.h
=======================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #ifndef _LIST_X_H_
  5. #define _LIST_X_H_

  6. #define assert(x)

  7. extern struct list_head;

  8. typedef struct node_s{
  9.     struct list_head list;
  10.     int id;
  11. }NODE_T;

  12. typedef struct list_s{
  13.     struct list_head head;
  14.     void (*prepend)(struct list_s *self_p, NODE_T *new_p);
  15.     void (*append)(struct list_s *self_p, NODE_T *new_p);
  16.     void (*delete)(struct list_s *self_p, NODE_T *node_p);
  17.     void (*replace)(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
  18.     NODE_T* (*lookup)(struct list_s *self_p, int id);
  19.     void (*print)(struct list_s *self_p);    
  20. }LIST_T;

  21. void listx_init(LIST_T *list_p);

  22. #define listx_prepend(list,node_p) ((list).prepend(&(list), node_p))
  23. #define listx_append(list,node_p) ((list).append(&(list), node_p))
  24. #define listx_print(list) ((list).print(&(list)))
  25. #define listx_replace(list,old_p,new_p) ((list).replace(&(list), old_p, new_p))
  26. #define listx_lookup(list,x) ((list).lookup(&(list), x))
  27. #define listx_delete(list,node_p) ((list).delete(&(list), node_p))

  28. #endif

文件:listx.c
=======================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #include "list.h"
  5. #include "listx.h"

  6. static void node_prepend(struct list_s *self_p, NODE_T *new_p);
  7. static void node_append(struct list_s *self_p, NODE_T *new_p);
  8. static void node_delete(struct list_s *self_p, NODE_T *node_p);
  9. static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
  10. static NODE_T* node_lookup(struct list_s *self_p, int id);
  11. static void node_print(struct list_s *self_p);

  12. static void node_prepend(struct list_s *self_p, NODE_T *new_p)
  13. {
  14.     assert(self_p && new_p);
  15.     list_add(&new_p->list, &(self_p->head));
  16. }

  17. static void node_append(struct list_s *self_p, NODE_T *new_p)
  18. {
  19.     assert(self_p && new_p);
  20.     list_add_tail(&new_p->list, &(self_p->head));
  21. }

  22. static void node_delete(struct list_s *self_p, NODE_T *node_p)
  23. {
  24.     assert(self_p && node_p);
  25.     list_del(&node_p->list);
  26. }

  27. static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p)
  28. {
  29.     assert(self_p && old_p && new_p);
  30.     list_replace(&old_p->list, &new_p->list);
  31. }

  32. static NODE_T* node_lookup(struct list_s *self_p, int id)
  33. {
  34.     struct list_head *list_p;
  35.     NODE_T *node_p = NULL;    
  36.     assert(self_p);
  37.     list_for_each(list_p, &(self_p->head))
  38.     {
  39.         node_p = list_entry(list_p, NODE_T, list);
  40.         if (node_p->id == id)
  41.             break;
  42.     }
  43.     return node_p;
  44. }

  45. static void node_print(struct list_s *self_p)
  46. {
  47.     struct list_head *list_p;
  48.     NODE_T *node_p;
  49.     list_for_each(list_p, &(self_p->head))
  50.     {
  51.         node_p = list_entry(list_p, NODE_T, list);
  52.         printf("node id=%d --> ", node_p->id);
  53.     }
  54.     printf("\r\n");
  55. }

  56. extern void listx_init(LIST_T *list_p)
  57. {
  58.     assert(list_p);
  59.     INIT_LIST_HEAD(&list_p->head);
  60.     *(list_p->prepend) = node_prepend;
  61.     *(list_p->append) = node_append;
  62.     *(list_p->delete) = node_delete;
  63.     *(list_p->replace) = node_replace;
  64.     *(list_p->lookup) = node_lookup;
  65.     *(list_p->print) = node_print;
  66. }

文件:main.c
=======================================================================================
  1. /*
  2.  * author: vincent.cws2008@gmail.com
  3.  */

  4. #include "list.h"
  5. #include "listx.h"

  6. void main()
  7. {
  8.     NODE_T *node_p = NULL;
  9.     NODE_T nodes[] =
  10.     {
  11.         {LIST_HEAD_INIT(nodes[0]), 0},
  12.         {LIST_HEAD_INIT(nodes[1]), 1},
  13.         {LIST_HEAD_INIT(nodes[2]), 2},
  14.         {LIST_HEAD_INIT(nodes[3]), 3},
  15.         {LIST_HEAD_INIT(nodes[4]), 4},
  16.         {LIST_HEAD_INIT(nodes[5]), 5},
  17.     };
  18.     
  19.     LIST_T list;
  20.     /* init */
  21.     listx_init(&list);
  22.     
  23.     /* prepend nodes */
  24.     listx_prepend(list, &nodes[4]);
  25.     listx_print(list);
  26.     listx_prepend(list, &nodes[3]);
  27.     listx_print(list);
  28.     /* append nodes */
  29.     listx_append(list, &nodes[0]);
  30.     listx_print(list);
  31.     listx_append(list, &nodes[1]);
  32.     listx_print(list);
  33.     /* prepend node */
  34.     listx_prepend(list, &nodes[2]);
  35.     listx_print(list);
  36.     /* replace node */
  37.     listx_replace(list, &nodes[1], &nodes[5]);
  38.     listx_print(list);
  39.     /* lookup node */
  40.     node_p = listx_lookup(list, 3);
  41.     if (node_p)
  42.     {
  43.         printf("lookup node id=%d\r\n", node_p->id);
  44.     }
  45.     /* delete nodes */
  46.     listx_delete(list, &nodes[4]);
  47.     listx_print(list);
  48.     listx_delete(list, &nodes[3]);
  49.     listx_print(list);
  50.     listx_delete(list, &nodes[2]);
  51.     listx_print(list);
  52.     listx_delete(list, &nodes[1]);
  53.     listx_print(list);
  54.     listx_delete(list, &nodes[0]);
  55.     listx_print(list);
  56. }




源代码如下:

 listx.rar 
阅读(444) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~