LinkedList1.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @file LinkedList1.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. * Simple doubly linked list.
  32. */
  33. #ifndef BADVPN_STRUCTURE_LINKEDLIST1_H
  34. #define BADVPN_STRUCTURE_LINKEDLIST1_H
  35. #include <stddef.h>
  36. #include <misc/debug.h>
  37. /**
  38. * Linked list node.
  39. */
  40. typedef struct LinkedList1Node_t
  41. {
  42. struct LinkedList1Node_t *p;
  43. struct LinkedList1Node_t *n;
  44. } LinkedList1Node;
  45. /**
  46. * Simple doubly linked list.
  47. */
  48. typedef struct
  49. {
  50. LinkedList1Node *first;
  51. LinkedList1Node *last;
  52. } LinkedList1;
  53. /**
  54. * Initializes the linked list.
  55. *
  56. * @param list list to initialize
  57. */
  58. static void LinkedList1_Init (LinkedList1 *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 LinkedList1_IsEmpty (LinkedList1 *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 LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list);
  73. /**
  74. * Returns the last node of the list.
  75. *
  76. * @param list the list
  77. * @return last node of the list, or NULL if the list is empty
  78. */
  79. static LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list);
  80. /**
  81. * Inserts a node to the beginning of the list.
  82. *
  83. * @param list the list
  84. * @param node uninitialized node to insert
  85. */
  86. static void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node);
  87. /**
  88. * Inserts a node to the end of the list.
  89. *
  90. * @param list the list
  91. * @param node uninitialized node to insert
  92. */
  93. static void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node);
  94. /**
  95. * Inserts a node before a given node.
  96. *
  97. * @param list the list
  98. * @param node uninitialized node to insert
  99. * @param target node in the list to insert before
  100. */
  101. static void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
  102. /**
  103. * Inserts a node after a given node.
  104. *
  105. * @param list the list
  106. * @param node uninitialized node to insert
  107. * @param target node in the list to insert after
  108. */
  109. static void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
  110. /**
  111. * Removes a node from the list.
  112. *
  113. * @param list the list
  114. * @param node node to remove
  115. */
  116. static void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node);
  117. /**
  118. * Inserts the nodes in the list \a ins_list into this list, after the node \a target.
  119. * If \a target is NULL, the nodes are inserted to the beginning.
  120. * Note that because the first and last node in \a ins_list are modified
  121. * (unless the list is empty), \a ins_list is invalidated and must no longer
  122. * be used to access the inserted nodes.
  123. */
  124. static void LinkedList1_InsertListAfter (LinkedList1 *list, LinkedList1 ins_list, LinkedList1Node *target);
  125. /**
  126. * Returns the next node of a given node.
  127. *
  128. * @param node reference node
  129. * @return next node, or NULL if none
  130. */
  131. static LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node);
  132. /**
  133. * Returns the previous node of a given node.
  134. *
  135. * @param node reference node
  136. * @return previous node, or NULL if none
  137. */
  138. static LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node);
  139. void LinkedList1_Init (LinkedList1 *list)
  140. {
  141. list->first = NULL;
  142. list->last = NULL;
  143. }
  144. int LinkedList1_IsEmpty (LinkedList1 *list)
  145. {
  146. return (!list->first);
  147. }
  148. LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list)
  149. {
  150. return (list->first);
  151. }
  152. LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list)
  153. {
  154. return (list->last);
  155. }
  156. void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node)
  157. {
  158. node->p = NULL;
  159. node->n = list->first;
  160. if (list->first) {
  161. list->first->p = node;
  162. } else {
  163. list->last = node;
  164. }
  165. list->first = node;
  166. }
  167. void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node)
  168. {
  169. node->p = list->last;
  170. node->n = NULL;
  171. if (list->last) {
  172. list->last->n = node;
  173. } else {
  174. list->first = node;
  175. }
  176. list->last = node;
  177. }
  178. void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  179. {
  180. node->p = target->p;
  181. node->n = target;
  182. if (target->p) {
  183. target->p->n = node;
  184. } else {
  185. list->first = node;
  186. }
  187. target->p = node;
  188. }
  189. void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  190. {
  191. node->p = target;
  192. node->n = target->n;
  193. if (target->n) {
  194. target->n->p = node;
  195. } else {
  196. list->last = node;
  197. }
  198. target->n = node;
  199. }
  200. void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node)
  201. {
  202. // remove from list
  203. if (node->p) {
  204. node->p->n = node->n;
  205. } else {
  206. list->first = node->n;
  207. }
  208. if (node->n) {
  209. node->n->p = node->p;
  210. } else {
  211. list->last = node->p;
  212. }
  213. }
  214. void LinkedList1_InsertListAfter (LinkedList1 *list, LinkedList1 ins_list, LinkedList1Node *target)
  215. {
  216. if (!ins_list.first) {
  217. return;
  218. }
  219. LinkedList1Node *t_next = (target ? target->n : list->first);
  220. ins_list.first->p = target;
  221. ins_list.last->n = t_next;
  222. if (target) {
  223. target->n = ins_list.first;
  224. } else {
  225. list->first = ins_list.first;
  226. }
  227. if (t_next) {
  228. t_next->p = ins_list.last;
  229. } else {
  230. list->last = ins_list.last;
  231. }
  232. }
  233. LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node)
  234. {
  235. return node->n;
  236. }
  237. LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node)
  238. {
  239. return node->p;
  240. }
  241. #endif