linkedlist2_example.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @file linkedlist2_example.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdint.h>
  24. #include <stddef.h>
  25. #include <structure/LinkedList2.h>
  26. struct elem
  27. {
  28. int i;
  29. LinkedList2Node list_node;
  30. };
  31. void printnode (LinkedList2Node *node)
  32. {
  33. if (!node) {
  34. printf("(null) ");
  35. } else {
  36. struct elem *e = (struct elem *)((uint8_t *)node-offsetof(struct elem, list_node));
  37. printf("%d ", e->i);
  38. }
  39. }
  40. void printall (LinkedList2 *list)
  41. {
  42. printf("List: ");
  43. LinkedList2Iterator it;
  44. LinkedList2Iterator_InitForward(&it, list);
  45. LinkedList2Node *node;
  46. while (node = LinkedList2Iterator_Next(&it)) {
  47. printnode(node);
  48. }
  49. printf("\n");
  50. }
  51. void removeall (LinkedList2 *list)
  52. {
  53. LinkedList2Iterator it;
  54. LinkedList2Iterator_InitForward(&it, list);
  55. LinkedList2Node *node;
  56. while (node = LinkedList2Iterator_Next(&it)) {
  57. LinkedList2_Remove(list, node);
  58. }
  59. }
  60. int main ()
  61. {
  62. struct elem elems[10];
  63. LinkedList2 list;
  64. LinkedList2_Init(&list);
  65. int i;
  66. for (i=0; i<10; i++) {
  67. elems[i].i = i;
  68. LinkedList2_Append(&list, &elems[i].list_node);
  69. }
  70. printall(&list);
  71. LinkedList2Iterator it1;
  72. LinkedList2Iterator it2;
  73. LinkedList2Iterator it3;
  74. LinkedList2Iterator it4;
  75. LinkedList2Iterator_InitForward(&it1, &list);
  76. LinkedList2Iterator_InitForward(&it2, &list);
  77. LinkedList2Iterator_InitBackward(&it3, &list);
  78. LinkedList2Iterator_InitBackward(&it4, &list);
  79. LinkedList2_Remove(&list, &elems[0].list_node);
  80. LinkedList2_Remove(&list, &elems[1].list_node);
  81. LinkedList2_Remove(&list, &elems[2].list_node);
  82. LinkedList2_Remove(&list, &elems[3].list_node);
  83. LinkedList2_Remove(&list, &elems[9].list_node);
  84. LinkedList2_Remove(&list, &elems[8].list_node);
  85. LinkedList2_Remove(&list, &elems[7].list_node);
  86. LinkedList2_Remove(&list, &elems[6].list_node);
  87. LinkedList2Node *node1;
  88. LinkedList2Node *node2;
  89. LinkedList2Node *node3;
  90. LinkedList2Node *node4;
  91. node1 = LinkedList2Iterator_Next(&it1);
  92. node2 = LinkedList2Iterator_Next(&it2);
  93. printnode(node1);
  94. printnode(node2);
  95. printf("\n");
  96. node3 = LinkedList2Iterator_Next(&it3);
  97. node4 = LinkedList2Iterator_Next(&it4);
  98. printnode(node3);
  99. printnode(node4);
  100. printf("\n");
  101. printall(&list);
  102. node1 = LinkedList2Iterator_Next(&it1);
  103. printnode(node1);
  104. printf("\n");
  105. node3 = LinkedList2Iterator_Next(&it3);
  106. printnode(node3);
  107. printf("\n");
  108. printall(&list);
  109. LinkedList2_Prepend(&list, &elems[3].list_node);
  110. LinkedList2_Append(&list, &elems[6].list_node);
  111. printall(&list);
  112. node1 = LinkedList2Iterator_Next(&it1);
  113. node2 = LinkedList2Iterator_Next(&it2);
  114. printnode(node1);
  115. printnode(node2);
  116. printf("\n");
  117. node3 = LinkedList2Iterator_Next(&it3);
  118. node4 = LinkedList2Iterator_Next(&it4);
  119. printnode(node3);
  120. printnode(node4);
  121. printf("\n");
  122. node1 = LinkedList2Iterator_Next(&it1);
  123. node2 = LinkedList2Iterator_Next(&it2);
  124. printnode(node1);
  125. printnode(node2);
  126. printf("\n");
  127. node3 = LinkedList2Iterator_Next(&it3);
  128. node4 = LinkedList2Iterator_Next(&it4);
  129. printnode(node3);
  130. printnode(node4);
  131. printf("\n");
  132. return 0;
  133. }