- 1 /*************************************************************************
- 2 > File Name: main.c
- 3 > Author: gaozy
- 4 > Mail:
- 5 > Created Time: 2016年12月24日 星期六 11时11分25秒
- 6 ************************************************************************/
- 7
- 8#include
- 9#include
- 10
- 11#include"dclist_head.h"
- 12
- 13typedefstruct {
- 14 int id;
- 15 dclist_head_t entry;
- 16 } student_t;
- 17
- 18 voidprint(dclist_head_t *ptr)
- 19 {
- 20student_t *stu = list_entry(ptr, student_t, entry);
- 21 if( stu == NULL )
- 22 return;
- 23printf("student id = %d\n", stu->id);
- 24 }
- 25
- 26 voidfree_node(dclist_head_t *ptr)
- 27 {
- 28 if(ptr == NULL )
- 29 return;
- 30
- 31student_t *node = list_entry(ptr, student_t, entry);
- 32 free(node);
- 33 }
- 34
- 35student_t* make_node(int id)
- 36 {
- 37student_t *stu = (student_t *)malloc(sizeof(student_t));
- 38 if( stu != NULL ) {
- 39stu->id = id;
- 40 }
- 41
- 42 return stu;
- 43 }
- 44
- 45 intmain(void)
- 46 {
- 47 dclist_head_t list;
- 48
- 49init(&list);
- 50
- 51 int i;
- 52student_t *stu;
- 53 for( i=0; i<5; i++ ) {
- 54stu = make_node(i);
- 55 if( stu != NULL )
- 56append(&list, &stu->entry);
- 57 }
- 58
- 59
- 60treaverse(&list, print);
- 61dc_remove(&list, free_node);
- 62
- 63
- 64 return 0;
- 65}
来源: