int main() { struct DList* list = dlist_create(); dlist_print(list); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 5); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 7); printf("list length = %d\n", dlist_length(list)); dlist_append(list, 8); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_remove(list, 1); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_insert(list, 0, 5); printf("list length = %d\n", dlist_length(list)); dlist_print(list); dlist_print_reverse(list); dlist_delete(list); return 0; }
int main() { int length = 0; int len = 0; // printf("please input list node num:"); // scanf("%d", &len); //int i = 'a'; //int i = 0; //Node* list = dlist_create(len, i); // dlist_set_print_callback(my_dlist_print_int); // dlist_print(list); //dlist_set_print_callback(my_dlist_print_char); //dlist_print(list); // length = dlist_get_length(list); // printf("lenght:%d\n", length); // dlist_for_each(list, upper_case_for_each, NULL); // dlist_print(list); // int index = 0; // int value =0; // printf("please input node index you want to insert:"); // scanf("%d", &index); // printf("please input node value you want to insert:"); // scanf("%d", &value); // dlist_insert(list,index, (void*)value); // dlist_print(list); // printf("please input node index you want to delete:"); // scanf("%d", &index); // dlist_delete(list, index); // dlist_print(list); // Node* max = dlist_get_max(list, dlist_get_max_node_int); // printf("max:"); // dlist_print_node(max); // int sum = 0; // dlist_for_each(list, sum_for_each, (void *)&sum); // printf("sum is %d\n", sum); int i = 0; Node* list = dlist_create(len, i); dlist_append(list, "hello"); dlist_append(list, "world"); dlist_append(list, "!"); dlist_set_print_callback(my_dlist_print_str); dlist_print(list); return 0; }
EXPORTED void dlist_printbuf(const struct dlist *dl, int printkeys, struct buf *outbuf) { struct protstream *outstream; outstream = prot_writebuf(outbuf); dlist_print(dl, printkeys, outstream); prot_flush(outstream); prot_free(outstream); }
int main(int argc, char* argv[]) { int i = 0; int n = 100; DList* dlist = dlist_create(); /*for(i = 0; i < n; i++) { assert(dlist_append(dlist, (void*)i) == DLIST_RET_OK); }*/ for(i = 0; i < n; i++) { assert(dlist_prepend(dlist, (void*)i) == DLIST_RET_OK); } dlist_print(dlist, print_int); dlist_destroy(dlist); return 0; }
int main(int argc, char* argv[]) { int i = 0; int num = 10; dlist* head = dlist_creat(); #ifdef TEST //#ifdef DLIST_CREAT_ARRAY //dlist_creat_array(); //#endif /*DLIST_CREAT_ARRAY*/ for (i = 0; i < num; i++) { /*dlist_ret ret = dlist_append(head, (void*)i); assert(ret == DLIST_RET_OK);*/ assert(dlist_append(head, (void*)i) == DLIST_RET_OK); /*dlist_append(head, (void*)i);*/ } /*for () { assert(dlist_prepend() == DLIST_RET_OK); } */ //dlist_insert(); // dlist_delete(); dlist_delete(head, 2); dlist_insert(head, 2 ,(void*)11); dlist_print(head, print_int); dlist_destroy(head); #endif /*TEST*/ }
EXPORTED void dlist_print(const struct dlist *dl, int printkeys, struct protstream *out) { struct dlist *di; if (printkeys) { prot_printastring(out, dl->name); prot_putc(' ', out); } switch (dl->type) { case DL_NIL: prot_printf(out, "NIL"); break; case DL_ATOM: prot_printastring(out, dl->sval); break; case DL_FLAG: prot_printf(out, "%s", dl->sval); break; case DL_NUM: case DL_DATE: /* for now, we will format it later */ prot_printf(out, "%llu", dl->nval); break; case DL_FILE: printfile(out, dl); break; case DL_BUF: if (strlen(dl->sval) == dl->nval) prot_printastring(out, dl->sval); else prot_printliteral(out, dl->sval, dl->nval); break; case DL_GUID: prot_printf(out, "%s", message_guid_encode(dl->gval)); break; case DL_HEX: { char buf[17]; snprintf(buf, 17, "%016llx", dl->nval); prot_printf(out, "%s", buf); } break; case DL_KVLIST: prot_printf(out, "%%("); for (di = dl->head; di; di = di->next) { dlist_print(di, 1, out); if (di->next) { prot_printf(out, " "); } } prot_printf(out, ")"); break; case DL_ATOMLIST: prot_printf(out, "("); for (di = dl->head; di; di = di->next) { dlist_print(di, dl->nval, out); if (di->next) prot_printf(out, " "); } prot_printf(out, ")"); break; } }
int test_dlist() { puts("##########################################"); puts("starting double linked list tests"); puts("##########################################"); int value = 0; struct DList *dlist = dlist_create(); puts("empty double list created"); if (dlist_length(dlist) != 0) { printf("dlist_length of empty list should be zero\n"); return 0; } puts("dlist_length ok"); // Insert value 101 and test functions dlist_insert(dlist, 0, 101); if (dlist_length(dlist) != 1) { printf("dlist_length should be 1\n"); return 0; } if (dlist_get(dlist, 0, &value) == 0) { printf("Error in dlist_get (1)\n"); return 0; } if (value != 101) { printf("dlist_get should return value 101\n"); return 0; } // Insert value 202 and test functions dlist_insert(dlist, 0, 202); if (dlist_length(dlist) != 2) { printf("dlist_length should return 2\n"); return 0; } if (dlist_get(dlist, 0, &value) == 0) { printf("Error in dlist_length (2)\n"); return 0; } if (value != 202) { printf("dlist_get should return 202\n"); return 0; } puts("dlist_get ok"); // Test remove function if (dlist_remove(dlist, 1) == 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 1) { printf("dlist_length should return 1 (after remove)\n"); return 0; } if (dlist_remove(dlist, 1) != 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 1) { printf("dlist_length should return 1 (after remove)\n"); return 0; } if (dlist_remove(dlist, 0) == 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 0) { printf("dlist_length should return 0 (after remove)\n"); return 0; } if (dlist_remove(dlist, 0) != 0) { printf("Error in dlist_remove\n"); return 0; } if (dlist_length(dlist) != 0) { printf("dlist_length should return 0 (after remove)\n"); return 0; } puts("dlist_remove ok"); // test dlist_append() dlist_append(dlist, -5); dlist_append(dlist, 1); dlist_append(dlist, 15); if (dlist_length(dlist) != 3) { printf("dlist_length should return 0\n"); return 0; } if (dlist_get(dlist, 0, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != -5) { printf("dlist_get should return -5\n"); return 0; } if (dlist_get(dlist, 1, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 1) { printf("dlist_get should return 1\n"); return 0; } if (dlist_get(dlist, 2, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 15) { printf("dlist_get should return 15\n"); return 0; } puts("dlist_append ok"); // test dlist insert dlist_insert(dlist, -5, 0); if (dlist_length(dlist) != 4) { printf("dlist_length should return 4\n"); return 0; } if (dlist_get(dlist, 0, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 0) { printf("dlist_get should return 0\n"); return 0; } dlist_insert(dlist, 1, 100); if (dlist_length(dlist) != 5) { printf("dlist_length should return 5\n"); return 0; } if (dlist_get(dlist, 1, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 100) { printf("dlist_get should return 100\n"); return 0; } dlist_insert(dlist, 10, 500); if (dlist_length(dlist) != 6) { printf("dlist_length should return 6\n"); return 0; } if (dlist_get(dlist, 5, &value) != 1) { printf("Error in dlist_append\n"); return 0; } if (value != 500) { printf("dlist_get should return 500\n"); return 0; } puts("dlist_insert ok"); // test print and print reversed puts("print current dlist"); dlist_print(dlist); puts("printing reversed dlist"); dlist_print_reverse(dlist); puts("check print and print_reversed for yourself!"); puts("##########################################"); puts("all tests of double linked lists completed"); puts("##########################################"); puts("------------------------------------------"); dlist_delete(dlist); return 1; }