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(" "); 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 }