#include #include #include "dl_list.h" // business logic for name list void delete_name(void* ptr); void print_name_list(dl_list* list); void add_name_to_node(dl_node* node, const char* name); void remove_name(dl_list* list, const char* name); void delete_node(dl_node* nd); int main(int argc, char** argv) { dl_list name_list; dl_node *nd; dl_node *tmp; dl_list_init(&name_list, delete_name); print_name_list(&name_list); nd = dl_list_push_front(&name_list); add_name_to_node(nd, "Bart"); print_name_list(&name_list); nd = dl_list_push_front(&name_list); add_name_to_node(nd, "Adam"); print_name_list(&name_list); nd = dl_list_push_back(&name_list); add_name_to_node(nd, "Carol"); print_name_list(&name_list); dl_list_clear(&name_list); print_name_list(&name_list); nd = dl_list_push_back(&name_list); add_name_to_node(nd, "Bart"); print_name_list(&name_list); nd = dl_list_push_back(&name_list); add_name_to_node(nd, "Carol"); print_name_list(&name_list); nd = dl_list_push_front(&name_list); add_name_to_node(nd, "Adam"); print_name_list(&name_list); nd = dl_list_pop_front(&name_list); delete_node(nd); print_name_list(&name_list); nd = dl_list_pop_back(&name_list); delete_node(nd); print_name_list(&name_list); nd = dl_list_pop_front(&name_list); delete_node(nd); print_name_list(&name_list); nd = dl_list_push_back(&name_list); add_name_to_node(nd, "Adam"); print_name_list(&name_list); nd = dl_list_pop_back(&name_list); delete_node(nd); print_name_list(&name_list); nd = dl_list_insert_before(&name_list, 0); add_name_to_node(nd, "Alexander"); print_name_list(&name_list); dl_list_remove_node(&name_list, nd); delete_node(nd); print_name_list(&name_list); nd = dl_list_insert_after(&name_list, 0); add_name_to_node(nd, "Beatrice"); print_name_list(&name_list); tmp = nd; nd = dl_list_insert_before(&name_list, tmp); add_name_to_node(nd, "Alexander"); print_name_list(&name_list); nd = dl_list_insert_after(&name_list, tmp); add_name_to_node(nd, "David"); print_name_list(&name_list); nd = dl_list_insert_after(&name_list, tmp); add_name_to_node(nd, "Ciscero"); print_name_list(&name_list); dl_list_reverse(&name_list); print_name_list(&name_list); dl_list_reverse(&name_list); print_name_list(&name_list); dl_list_remove_node(&name_list, tmp); delete_node(tmp); print_name_list(&name_list); tmp = name_list.tail; dl_list_remove_node(&name_list, tmp); delete_node(tmp); print_name_list(&name_list); tmp = name_list.head; dl_list_remove_node(&name_list, tmp); delete_node(tmp); print_name_list(&name_list); dl_list_destroy(&name_list); return 0; } void delete_node(dl_node* nd) { free(nd->data); free(nd); } void delete_name(void* ptr) { free(ptr); } void print_name_list(dl_list* list) { fprintf(stdout, "-----------------------\n"); fprintf(stdout, "size = %d -- empty = %d\n", dl_list_size(list), dl_list_empty(list)); dl_node* iter = list->head; while(iter) { fprintf(stdout, "%s\n", (char*)iter->data); iter = iter->next; } fprintf(stdout, "-----------------------\n"); } void add_name_to_node(dl_node* node, const char* name) { char* node_string; if (!node) { fprintf(stderr, "error on memory allocation\n"); exit(1); } node->data = malloc(strlen(name) + 1); if (!node->data) { fprintf(stderr, "error on memory allocation\n"); exit(1); } node_string = (char*)node->data; strcpy(node_string, name); } void remove_name(dl_list* list, const char* name) { char* str; dl_node *iter = list->head; while(iter) { if (strcmp((char*)iter->data, name) == 0 ) break; iter = iter->next; } // found the item that needs to be removed if (iter) { dl_list_remove_node(list, iter); free(iter->data); free(iter); } }