test-list.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Doubly-linked list - test program
  3. * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/os.h"
  16. #include "utils/list.h"
  17. struct test {
  18. struct dl_list list;
  19. int value;
  20. };
  21. static void dump_list(struct dl_list *head)
  22. {
  23. struct test *t;
  24. printf("dump:");
  25. dl_list_for_each(t, head, struct test, list)
  26. printf(" %d", t->value);
  27. printf(" (len=%d%s)\n", dl_list_len(head),
  28. dl_list_empty(head) ? " empty" : "");
  29. }
  30. int main(int argc, char *argv[])
  31. {
  32. struct dl_list head;
  33. struct test *t, *tmp;
  34. int i;
  35. dl_list_init(&head);
  36. dump_list(&head);
  37. for (i = 0; i < 5; i++) {
  38. t = os_zalloc(sizeof(*t));
  39. if (t == NULL)
  40. return -1;
  41. t->value = i;
  42. dl_list_add(&head, &t->list);
  43. dump_list(&head);
  44. }
  45. for (i = 10; i > 5; i--) {
  46. t = os_zalloc(sizeof(*t));
  47. if (t == NULL)
  48. return -1;
  49. t->value = i;
  50. dl_list_add_tail(&head, &t->list);
  51. dump_list(&head);
  52. }
  53. i = 0;
  54. dl_list_for_each(t, &head, struct test, list)
  55. if (++i == 5)
  56. break;
  57. printf("move: %d\n", t->value);
  58. dl_list_del(&t->list);
  59. dl_list_add(&head, &t->list);
  60. dump_list(&head);
  61. dl_list_for_each_safe(t, tmp, &head, struct test, list) {
  62. printf("delete: %d\n", t->value);
  63. dl_list_del(&t->list);
  64. os_free(t);
  65. dump_list(&head);
  66. }
  67. return 0;
  68. }