厚德博学 敬业乐群
@sky
全部博文(252)
2015年(2)
2014年(1)
2013年(1)
2012年(16)
2011年(42)
2010年(67)
2009年(87)
2008年(36)
25742040
shijiulo
niuxlinu
ebayboy
hayand66
大鬼不动
acer1025
醉鬼的故
小雅贝贝
XINGCHEN
wzy_yzw
十的9次
zds05
bjywxc
zlhc1
smile124
cynthia
格伯纳
分类: LINUX
2008-03-31 08:32:22
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stddef.h> #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (char *)__mptr - offsetof(type,member) );}) struct list_head { struct list_head *prev; struct list_head *next; }; struct member_info { int number; struct list_head list; }; static void init_list_head(struct list_head *head) { head->prev = head->next = head; } static void list_add(struct list_head *new, struct list_head *head) { struct list_head *prev = head, *next = head->next; next->prev = new; new->next = next; prev->next = new; new->prev = prev; } static void list_travel(struct list_head *head) { struct list_head *p = NULL; struct member_info *info = NULL; for (p = head->next; p != head; p = p->next) { info = container_of(p, struct member_info, list); printf("%d->", info->number); } printf("NULL\n"); } static void list_destroy(struct list_head *head) { struct list_head *prev = NULL, *next = NULL; struct member_info *info = NULL; next = head->next; while (next != head) { prev = next; next = next->next; info = container_of(prev, struct member_info, list); free(info); } } int main(void) { struct list_head head; struct member_info *node = NULL; init_list_head(&head); for (int i = 0; i < 100; ++i) { node = malloc(sizeof(struct member_info)); memset(node, '\0', sizeof(struct member_info)); node->number = i; list_add(&node->list, &head); } list_travel(&head); list_destroy(&head); return 0; }
上一篇:workqueue用法
下一篇:循环链表
登录 注册