- 论坛徽章:
- 0
|
- /*
- * =====================================================================================
- *
- * 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[20];
- struct list_head dog_list;
- struct list_head cat_list;
- };
- struct dog {
- char name[20];
- struct list_head dog_list;
- };
- struct cat {
- char name[20];
- 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;
- }
复制代码 |
|