首页 > 系统服务 > 详细

Linux内核之旅 链表实现

时间:2014-05-05 10:09:58      阅读:435      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
 1 #include "stdio.h"
 2 #include "stdlib.h"
 3 
 4 struct list_head{
 5     struct list_head *prev;
 6     struct list_head *next;
 7 };
 8 
 9 struct task{
10     int member;
11     struct list_head list;
12 };
13 
14 #define list_entry(ptr,member,type) 15 ((type*)((char *)ptr-(unsigned long)&(((type*)0)->member)))
16 
17 #define Init_list(list) {&list,&list}
18 
19  static  void list_add(struct list_head* ptr,struct list_head* n){
20     ptr->next=n->next;
21     n->next->prev=ptr;
22     n->next=ptr;
23     ptr->prev=n;
24 }
25 
26 #define for_each(list_)27 for(pos=list_.next;pos!=&list_;pos=pos->next)28 {printf("%d ",list_entry(pos,list,struct task)->member);}29 printf("\n");
30 
31 
32 void main(){
33     struct task* tmp;
34     int i;
35     struct list_head mylist=Init_list(mylist),*pos;
36 
37     for( i=0;i<5;i++){
38         tmp=(struct task*)malloc(sizeof(struct task));
39         scanf("%d",&(tmp->member));
40         list_add(&(tmp->list),&mylist);
41     }
42     
43     for_each(mylist);
44 
45 }
bubuko.com,布布扣

 

Linux内核之旅 链表实现,布布扣,bubuko.com

Linux内核之旅 链表实现

原文:http://www.cnblogs.com/593213556wuyubao/p/3707891.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!