int main () { Dlist<int> mylist; Dlist<int>::node_type *it1; Dlist<int>::node_type *it2; // set some values: for (int i=1; i<10; ++i) mylist.insert_tail(new Dlist<int>::node_type(i*10)); // 10 20 30 40 50 60 70 80 90 it1 = it2 = mylist.head(); // ^^ for (int i = 0; i < 6; i++) it2 = dlist_next(it2); // ^ ^ it1 = dlist_next(it1); // ^ ^ it1 = mylist.erase(it1); // 10 30 40 50 60 70 80 90 // ^ ^ it2 = mylist.erase(it2); // 10 30 40 50 60 80 90 // ^ ^ it1 = dlist_next(it1); // ^ ^ it2 = dlist_prev(it2); // ^ ^ mylist.erase(it1, dlist_prev(it2)); // 10 30 60 80 90 // ^ std::cout << "mylist contains:"; for (it1=mylist.head(); it1!=mylist.nil(); it1 = dlist_next(it1)) std::cout << ' ' << it1->key; std::cout << '\n'; return 0; }
static inline int dlist_attach(Dlist *dl, Dlist_data *inserted, Dlist_data *inserted_n) { Dlist_data *inserted_p = dlist_prev(inserted_n); dlist_next(inserted_p) = inserted; dlist_prev(inserted) = inserted_p; dlist_next(inserted) = inserted_n; dlist_prev(inserted_n) = inserted; dlist_size(dl)++; return 1; }
void slip_release(pSlip gd) { DLElement *e; e = dlist_tail(gd->lstObjects); while (e != NULL) { pSlipObject obj; obj = dlist_data(e); e = dlist_prev(e); s_ReleaseObject(gd, obj); } FreeDList(gd->lstObjects); FreeDList(gd->lstSymbols); FreeDList(gd->lstStrings); FreeDList(gd->lstGlobalEnvironment); FreeDList(gd->parse_data.lstTokens); free(gd); }
static inline int dlist_detach(Dlist *dl, Dlist_data *dd) { Dlist_data *dd_p, *dd_n; if (__dlist_guard(dl) == dd) return 0; dd_p = dlist_prev(dd); dd_n = dlist_next(dd); dlist_next(dd_p) = dd_n; dlist_prev(dd_n) = dd_p; dlist_size(dl)--; return 1; }
int dlist_destroy(Dlist *dl) { Dlist_data *dd, *dd_n, *g; g = __dlist_guard(dl); dlist_next(dlist_prev(g)) = NULL; dd_n = dlist_next(g); free(g); for (dd = dd_n; dd; dd = dd_n) { dd_n = dlist_next(dd); destroy_dlist_data(dd); free(dd); } free(dl); return 1; }
Dlist * dlist_create(void) { Dlist *dl = calloc(1, sizeof(Dlist)); if (unlikely(dl == NULL)) goto error; dl->guard = dlist_data_create(dl); if (unlikely(dl->guard == NULL)) goto error; dlist_prev(dl->guard) = dl->guard; dlist_next(dl->guard) = dl->guard; return dl; error: if (dl) free(dl); return NULL; }
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; }