#include #include "list.h" #include #include #define NAME_LEN 16 /* A meaningless structure to put on the list */ typedef struct entity_type { char e_name[NAME_LEN]; // entity name int e_id; // entity id code } e_t; int main() { list_t *elist; link_t *elink; e_t *eloc; char name[NAME_LEN]; int count; /* Create new list */ elist = list_init(); /* Read input file consisting of names and id codes adding entities to list */ while (scanf("%s %d", name, &count) == 2) { eloc = malloc(sizeof(e_t)); strcpy(eloc->e_name, name); eloc->e_id = count; list_add(elist, (void *)eloc); } /* Now play it back */ list_reset(elist); while (list_not_end(elist)) { eloc = (e_t *)list_get_entity(elist); printf("%s %d \n", eloc->e_name, eloc->e_id); list_next_link(elist); } /* Try it again... */ list_reset(elist); while (list_not_end(elist)) { eloc = (e_t *)list_get_entity(elist); printf("%s %d \n", eloc->e_name, eloc->e_id); list_next_link(elist); } /* Now free all list control structures and the */ /* e_t structures as well. */ list_del(elist); /* See if we trashed malloc's control structures */ elist = list_init(); /* Nope ... see if we can delete an empty list */ list_del(elist); /* prove we survived */ printf("done\n"); }