http://
vincent.blog.chinaunix.net今天有空,就把一个C函数的链表封装一下,附件放到最后:
文件:listx.h
=======================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #ifndef _LIST_X_H_
-
#define _LIST_X_H_
-
-
#define assert(x)
-
-
extern struct list_head;
-
-
typedef struct node_s{
-
struct list_head list;
-
int id;
-
}NODE_T;
-
-
typedef struct list_s{
-
struct list_head head;
-
void (*prepend)(struct list_s *self_p, NODE_T *new_p);
-
void (*append)(struct list_s *self_p, NODE_T *new_p);
-
void (*delete)(struct list_s *self_p, NODE_T *node_p);
-
void (*replace)(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
-
NODE_T* (*lookup)(struct list_s *self_p, int id);
-
void (*print)(struct list_s *self_p);
-
}LIST_T;
-
-
void listx_init(LIST_T *list_p);
-
-
#define listx_prepend(list,node_p) ((list).prepend(&(list), node_p))
-
#define listx_append(list,node_p) ((list).append(&(list), node_p))
-
#define listx_print(list) ((list).print(&(list)))
-
#define listx_replace(list,old_p,new_p) ((list).replace(&(list), old_p, new_p))
-
#define listx_lookup(list,x) ((list).lookup(&(list), x))
-
#define listx_delete(list,node_p) ((list).delete(&(list), node_p))
-
-
#endif
文件:listx.c
=======================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #include "list.h"
-
#include "listx.h"
-
-
static void node_prepend(struct list_s *self_p, NODE_T *new_p);
-
static void node_append(struct list_s *self_p, NODE_T *new_p);
-
static void node_delete(struct list_s *self_p, NODE_T *node_p);
-
static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p);
-
static NODE_T* node_lookup(struct list_s *self_p, int id);
-
static void node_print(struct list_s *self_p);
-
-
static void node_prepend(struct list_s *self_p, NODE_T *new_p)
-
{
-
assert(self_p && new_p);
-
list_add(&new_p->list, &(self_p->head));
-
}
-
-
static void node_append(struct list_s *self_p, NODE_T *new_p)
-
{
-
assert(self_p && new_p);
-
list_add_tail(&new_p->list, &(self_p->head));
-
}
-
-
static void node_delete(struct list_s *self_p, NODE_T *node_p)
-
{
-
assert(self_p && node_p);
-
list_del(&node_p->list);
-
}
-
-
static void node_replace(struct list_s *self_p, NODE_T *old_p, NODE_T *new_p)
-
{
-
assert(self_p && old_p && new_p);
-
list_replace(&old_p->list, &new_p->list);
-
}
-
-
static NODE_T* node_lookup(struct list_s *self_p, int id)
-
{
-
struct list_head *list_p;
-
NODE_T *node_p = NULL;
-
assert(self_p);
-
list_for_each(list_p, &(self_p->head))
-
{
-
node_p = list_entry(list_p, NODE_T, list);
-
if (node_p->id == id)
-
break;
-
}
-
return node_p;
-
}
-
-
static void node_print(struct list_s *self_p)
-
{
-
struct list_head *list_p;
-
NODE_T *node_p;
-
list_for_each(list_p, &(self_p->head))
-
{
-
node_p = list_entry(list_p, NODE_T, list);
-
printf("node id=%d --> ", node_p->id);
-
}
-
printf("\r\n");
-
}
-
-
extern void listx_init(LIST_T *list_p)
-
{
-
assert(list_p);
-
INIT_LIST_HEAD(&list_p->head);
-
*(list_p->prepend) = node_prepend;
-
*(list_p->append) = node_append;
-
*(list_p->delete) = node_delete;
-
*(list_p->replace) = node_replace;
-
*(list_p->lookup) = node_lookup;
-
*(list_p->print) = node_print;
-
}
文件:main.c
=======================================================================================
- /*
- * author: vincent.cws2008@gmail.com
- */
- #include "list.h"
-
#include "listx.h"
-
-
void main()
-
{
-
NODE_T *node_p = NULL;
-
NODE_T nodes[] =
-
{
-
{LIST_HEAD_INIT(nodes[0]), 0},
-
{LIST_HEAD_INIT(nodes[1]), 1},
-
{LIST_HEAD_INIT(nodes[2]), 2},
-
{LIST_HEAD_INIT(nodes[3]), 3},
-
{LIST_HEAD_INIT(nodes[4]), 4},
-
{LIST_HEAD_INIT(nodes[5]), 5},
-
};
-
-
LIST_T list;
-
/* init */
-
listx_init(&list);
-
-
/* prepend nodes */
-
listx_prepend(list, &nodes[4]);
-
listx_print(list);
-
listx_prepend(list, &nodes[3]);
-
listx_print(list);
-
/* append nodes */
-
listx_append(list, &nodes[0]);
-
listx_print(list);
-
listx_append(list, &nodes[1]);
-
listx_print(list);
-
/* prepend node */
-
listx_prepend(list, &nodes[2]);
-
listx_print(list);
-
/* replace node */
-
listx_replace(list, &nodes[1], &nodes[5]);
-
listx_print(list);
-
/* lookup node */
-
node_p = listx_lookup(list, 3);
-
if (node_p)
-
{
-
printf("lookup node id=%d\r\n", node_p->id);
-
}
-
/* delete nodes */
-
listx_delete(list, &nodes[4]);
-
listx_print(list);
-
listx_delete(list, &nodes[3]);
-
listx_print(list);
-
listx_delete(list, &nodes[2]);
-
listx_print(list);
-
listx_delete(list, &nodes[1]);
-
listx_print(list);
-
listx_delete(list, &nodes[0]);
-
listx_print(list);
-
}
源代码如下:
listx.rar
阅读(650) | 评论(0) | 转发(0) |