list_head理解
/** =====================================================================================
*
* Filename:demo.c
*
* Description:
*
* Version:1.0
* Created:10/24/2012 09:32:10 AM
* Revision:none
* Compiler:gcc
*
* Author:scar (gao), fevergao@gmail.com
* Organization:Zecloud.Inc
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
struct person {
char name;
struct list_head dog_list;
struct list_head cat_list;
};
struct dog {
char name;
struct list_head dog_list;
};
struct cat {
char name;
struct list_head cat_list;
};
int main(int argc, const char *argv[])
{
struct person person;
struct dog *tmp_dog = NULL;
struct dog *tmp = NULL;
struct dog *next = NULL;
struct cat *tmp_cat = NULL;
struct cat *tmp1 = NULL;
struct cat *next1 = NULL;
int i = 0;
strcpy (person.name, "scar");
INIT_LIST_HEAD (&person.dog_list);
INIT_LIST_HEAD (&person.cat_list);
for (i = 0; i < 5; i++) {
tmp_dog = malloc (sizeof (*tmp_dog));
tmp_cat = malloc (sizeof (*tmp_cat));
if (!tmp_dog)
return -1;
if (!tmp_cat)
return -1;
sprintf (tmp_dog->name, "%c", 'a' + i);
sprintf (tmp_cat->name, "%c", 'm' + i);
list_add_tail (&tmp_dog->dog_list, &person.dog_list);
list_add_tail (&tmp_cat->cat_list, &person.cat_list);
}
list_for_each_entry (tmp, &person.dog_list, dog_list) {
printf ("tmp->name=%s\n",tmp->name);
}
list_for_each_entry (tmp1 ,&person.cat_list, cat_list) {
printf ("---tmp1->name=%s\n",tmp1->name);
}
#if 1
list_for_each_entry_safe (tmp, next, &person.dog_list, dog_list) {
list_del (&tmp->dog_list);
free (tmp);
}
list_for_each_entry_safe (tmp1,next1, &person.cat_list, cat_list) {
list_del (&tmp1->cat_list);
free (tmp1);
}
#endif
return 0;
}
神马意思... list的声明和实现呢... 涉及到链表操作的话,多用list.h吧,至少会显得“专业”一点。。:mrgreen: 学习了下,好的
页:
[1]