LinkedList0.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @file LinkedList0.h
  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. * @section DESCRIPTION
  30. *
  31. * Very simple doubly linked list, with only a 'first' pointer an no 'last'
  32. * pointer.
  33. */
  34. #ifndef BADVPN_STRUCTURE_LINKEDLIST0_H
  35. #define BADVPN_STRUCTURE_LINKEDLIST0_H
  36. #include <stddef.h>
  37. #include <misc/debug.h>
  38. /**
  39. * Linked list node.
  40. */
  41. typedef struct LinkedList0Node_t
  42. {
  43. struct LinkedList0Node_t *p;
  44. struct LinkedList0Node_t *n;
  45. } LinkedList0Node;
  46. /**
  47. * Simple doubly linked list.
  48. */
  49. typedef struct
  50. {
  51. LinkedList0Node *first;
  52. } LinkedList0;
  53. /**
  54. * Initializes the linked list.
  55. *
  56. * @param list list to initialize
  57. */
  58. static void LinkedList0_Init (LinkedList0 *list);
  59. /**
  60. * Determines if the list is empty.
  61. *
  62. * @param list the list
  63. * @return 1 if empty, 0 if not
  64. */
  65. static int LinkedList0_IsEmpty (LinkedList0 *list);
  66. /**
  67. * Returns the first node of the list.
  68. *
  69. * @param list the list
  70. * @return first node of the list, or NULL if the list is empty
  71. */
  72. static LinkedList0Node * LinkedList0_GetFirst (LinkedList0 *list);
  73. /**
  74. * Inserts a node to the beginning of the list.
  75. *
  76. * @param list the list
  77. * @param node uninitialized node to insert
  78. */
  79. static void LinkedList0_Prepend (LinkedList0 *list, LinkedList0Node *node);
  80. /**
  81. * Inserts a node before a given node.
  82. *
  83. * @param list the list
  84. * @param node uninitialized node to insert
  85. * @param target node in the list to insert before
  86. */
  87. static void LinkedList0_InsertBefore (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target);
  88. /**
  89. * Inserts a node after a given node.
  90. *
  91. * @param list the list
  92. * @param node uninitialized node to insert
  93. * @param target node in the list to insert after
  94. */
  95. static void LinkedList0_InsertAfter (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target);
  96. /**
  97. * Removes a node from the list.
  98. *
  99. * @param list the list
  100. * @param node node to remove
  101. */
  102. static void LinkedList0_Remove (LinkedList0 *list, LinkedList0Node *node);
  103. /**
  104. * Returns the next node of a given node.
  105. *
  106. * @param node reference node
  107. * @return next node, or NULL if none
  108. */
  109. static LinkedList0Node * LinkedList0Node_Next (LinkedList0Node *node);
  110. /**
  111. * Returns the previous node of a given node.
  112. *
  113. * @param node reference node
  114. * @return previous node, or NULL if none
  115. */
  116. static LinkedList0Node * LinkedList0Node_Prev (LinkedList0Node *node);
  117. void LinkedList0_Init (LinkedList0 *list)
  118. {
  119. list->first = NULL;
  120. }
  121. int LinkedList0_IsEmpty (LinkedList0 *list)
  122. {
  123. return (!list->first);
  124. }
  125. LinkedList0Node * LinkedList0_GetFirst (LinkedList0 *list)
  126. {
  127. return (list->first);
  128. }
  129. void LinkedList0_Prepend (LinkedList0 *list, LinkedList0Node *node)
  130. {
  131. node->p = NULL;
  132. node->n = list->first;
  133. if (list->first) {
  134. list->first->p = node;
  135. }
  136. list->first = node;
  137. }
  138. void LinkedList0_InsertBefore (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target)
  139. {
  140. node->p = target->p;
  141. node->n = target;
  142. if (target->p) {
  143. target->p->n = node;
  144. } else {
  145. list->first = node;
  146. }
  147. target->p = node;
  148. }
  149. void LinkedList0_InsertAfter (LinkedList0 *list, LinkedList0Node *node, LinkedList0Node *target)
  150. {
  151. node->p = target;
  152. node->n = target->n;
  153. if (target->n) {
  154. target->n->p = node;
  155. }
  156. target->n = node;
  157. }
  158. void LinkedList0_Remove (LinkedList0 *list, LinkedList0Node *node)
  159. {
  160. // remove from list
  161. if (node->p) {
  162. node->p->n = node->n;
  163. } else {
  164. list->first = node->n;
  165. }
  166. if (node->n) {
  167. node->n->p = node->p;
  168. }
  169. }
  170. LinkedList0Node * LinkedList0Node_Next (LinkedList0Node *node)
  171. {
  172. return node->n;
  173. }
  174. LinkedList0Node * LinkedList0Node_Prev (LinkedList0Node *node)
  175. {
  176. return node->p;
  177. }
  178. #endif