linkedlist2_example.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @file linkedlist2_example.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdint.h>
  31. #include <stddef.h>
  32. #include <structure/LinkedList2.h>
  33. struct elem
  34. {
  35. int i;
  36. LinkedList2Node list_node;
  37. };
  38. void printnode (LinkedList2Node *node)
  39. {
  40. if (!node) {
  41. printf("(null) ");
  42. } else {
  43. struct elem *e = (struct elem *)((uint8_t *)node-offsetof(struct elem, list_node));
  44. printf("%d ", e->i);
  45. }
  46. }
  47. void printall (LinkedList2 *list)
  48. {
  49. printf("List: ");
  50. LinkedList2Iterator it;
  51. LinkedList2Iterator_InitForward(&it, list);
  52. LinkedList2Node *node;
  53. while (node = LinkedList2Iterator_Next(&it)) {
  54. printnode(node);
  55. }
  56. printf("\n");
  57. }
  58. void removeall (LinkedList2 *list)
  59. {
  60. LinkedList2Iterator it;
  61. LinkedList2Iterator_InitForward(&it, list);
  62. LinkedList2Node *node;
  63. while (node = LinkedList2Iterator_Next(&it)) {
  64. LinkedList2_Remove(list, node);
  65. }
  66. }
  67. int main ()
  68. {
  69. struct elem elems[10];
  70. LinkedList2 list;
  71. LinkedList2_Init(&list);
  72. int i;
  73. for (i=0; i<10; i++) {
  74. elems[i].i = i;
  75. LinkedList2_Append(&list, &elems[i].list_node);
  76. }
  77. printall(&list);
  78. LinkedList2Iterator it1;
  79. LinkedList2Iterator it2;
  80. LinkedList2Iterator it3;
  81. LinkedList2Iterator it4;
  82. LinkedList2Iterator_InitForward(&it1, &list);
  83. LinkedList2Iterator_InitForward(&it2, &list);
  84. LinkedList2Iterator_InitBackward(&it3, &list);
  85. LinkedList2Iterator_InitBackward(&it4, &list);
  86. LinkedList2_Remove(&list, &elems[0].list_node);
  87. LinkedList2_Remove(&list, &elems[1].list_node);
  88. LinkedList2_Remove(&list, &elems[2].list_node);
  89. LinkedList2_Remove(&list, &elems[3].list_node);
  90. LinkedList2_Remove(&list, &elems[9].list_node);
  91. LinkedList2_Remove(&list, &elems[8].list_node);
  92. LinkedList2_Remove(&list, &elems[7].list_node);
  93. LinkedList2_Remove(&list, &elems[6].list_node);
  94. LinkedList2Node *node1;
  95. LinkedList2Node *node2;
  96. LinkedList2Node *node3;
  97. LinkedList2Node *node4;
  98. node1 = LinkedList2Iterator_Next(&it1);
  99. node2 = LinkedList2Iterator_Next(&it2);
  100. printnode(node1);
  101. printnode(node2);
  102. printf("\n");
  103. node3 = LinkedList2Iterator_Next(&it3);
  104. node4 = LinkedList2Iterator_Next(&it4);
  105. printnode(node3);
  106. printnode(node4);
  107. printf("\n");
  108. printall(&list);
  109. node1 = LinkedList2Iterator_Next(&it1);
  110. printnode(node1);
  111. printf("\n");
  112. node3 = LinkedList2Iterator_Next(&it3);
  113. printnode(node3);
  114. printf("\n");
  115. printall(&list);
  116. LinkedList2_Prepend(&list, &elems[3].list_node);
  117. LinkedList2_Append(&list, &elems[6].list_node);
  118. printall(&list);
  119. node1 = LinkedList2Iterator_Next(&it1);
  120. node2 = LinkedList2Iterator_Next(&it2);
  121. printnode(node1);
  122. printnode(node2);
  123. printf("\n");
  124. node3 = LinkedList2Iterator_Next(&it3);
  125. node4 = LinkedList2Iterator_Next(&it4);
  126. printnode(node3);
  127. printnode(node4);
  128. printf("\n");
  129. node1 = LinkedList2Iterator_Next(&it1);
  130. node2 = LinkedList2Iterator_Next(&it2);
  131. printnode(node1);
  132. printnode(node2);
  133. printf("\n");
  134. node3 = LinkedList2Iterator_Next(&it3);
  135. node4 = LinkedList2Iterator_Next(&it4);
  136. printnode(node3);
  137. printnode(node4);
  138. printf("\n");
  139. return 0;
  140. }