int visit(struct bitree_node *node) { struct dlist *list; if (!visit_list) { visit_list = (struct dlist *) malloc(sizeof(struct dlist)); dlist_init(visit_list, NULL); } list = visit_list; if (!node || !node->data) return -1; dlist_ins_next(list, dlist_tail(list), (const void *) node->data); return 0; }
SERVER *server_add(char *host, u_short port, char ipv6) { SERVER *server; if (port == 0 || host == NULL) return NULL; server = malloc(sizeof(SERVER)); bzero(server, sizeof(SERVER)); strncpy(server->host, host, MAX_HOSTNAME_LEN - 1); server->host[MAX_HOSTNAME_LEN - 1] = '\0'; server->port = port; #ifdef IPV6 server->ipv6 = ipv6; #endif dlist_ins_next(&e_global->servers, dlist_tail(&e_global->servers), server); log(LOG_INIT_DEBUG, "Server %s (Port: %i) has been added.", server->host, server->port); return server; }
int main(int argc, char **argv) { DList list; DListElmt *element; int *data, i; /***************************************************************************** * * * Initialize the doubly-linked list. * * * *****************************************************************************/ dlist_init(&list, free); /***************************************************************************** * * * Perform some doubly-linked list operations. * * * *****************************************************************************/ element = dlist_head(&list); for (i = 10; i > 0; i--) { if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = i; if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; } print_list(&list); element = dlist_head(&list); for (i = 0; i < 8; i++) element = dlist_next(element); data = dlist_data(element); fprintf(stdout, "Removing an element after the one containing %03d\n", *data); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 011 at the tail of the list\n"); *data = 11; if (dlist_ins_next(&list, dlist_tail(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Removing an element at the tail of the list\n"); element = dlist_tail(&list); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 012 just before the tail of the list\n"); *data = 12; if (dlist_ins_prev(&list, dlist_tail(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Iterating and removing the fourth element\n"); element = dlist_head(&list); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_prev(element); element = dlist_next(element); element = dlist_next(element); element = dlist_next(element); if (dlist_remove(&list, element, (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 013 before the first element\n"); *data = 13; if (dlist_ins_prev(&list, dlist_head(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Removing an element at the head of the list\n"); if (dlist_remove(&list, dlist_head(&list), (void **)&data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 014 just after the head of the list\n"); *data = 14; if (dlist_ins_next(&list, dlist_head(&list), data) != 0) return 1; print_list(&list); fprintf(stdout, "Inserting 015 two elements after the head of the list\n"); if ((data = (int *)malloc(sizeof(int))) == NULL) return 1; *data = 15; element = dlist_head(&list); element = dlist_next(element); if (dlist_ins_next(&list, element, data) != 0) return 1; print_list(&list); i = dlist_is_head(dlist_head(&list)); fprintf(stdout, "Testing dlist_is_head...Value=%d (1=OK)\n", i); i = dlist_is_head(dlist_tail(&list)); fprintf(stdout, "Testing dlist_is_head...Value=%d (0=OK)\n", i); i = dlist_is_tail(dlist_tail(&list)); fprintf(stdout, "Testing dlist_is_tail...Value=%d (1=OK)\n", i); i = dlist_is_tail(dlist_head(&list)); fprintf(stdout, "Testing dlist_is_tail...Value=%d (0=OK)\n", i); /***************************************************************************** * * * Destroy the doubly-linked list. * * * *****************************************************************************/ fprintf(stdout, "Destroying the list\n"); dlist_destroy(&list); return 0; }
int enqueue(Queue *queue, const void *data) { return dlist_ins_next(queue, dlist_tail(queue), data); }