LinkedList1.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * Returns the next node of a given node.
  119. *
  120. * @param node reference node
  121. * @return next node, or NULL if none
  122. */
  123. static LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node);
  124. /**
  125. * Returns the previous node of a given node.
  126. *
  127. * @param node reference node
  128. * @return previous node, or NULL if none
  129. */
  130. static LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node);
  131. void LinkedList1_Init (LinkedList1 *list)
  132. {
  133. list->first = NULL;
  134. list->last = NULL;
  135. }
  136. int LinkedList1_IsEmpty (LinkedList1 *list)
  137. {
  138. return (!list->first);
  139. }
  140. LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list)
  141. {
  142. return (list->first);
  143. }
  144. LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list)
  145. {
  146. return (list->last);
  147. }
  148. void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node)
  149. {
  150. node->p = NULL;
  151. node->n = list->first;
  152. if (list->first) {
  153. list->first->p = node;
  154. } else {
  155. list->last = node;
  156. }
  157. list->first = node;
  158. }
  159. void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node)
  160. {
  161. node->p = list->last;
  162. node->n = NULL;
  163. if (list->last) {
  164. list->last->n = node;
  165. } else {
  166. list->first = node;
  167. }
  168. list->last = node;
  169. }
  170. void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  171. {
  172. node->p = target->p;
  173. node->n = target;
  174. if (target->p) {
  175. target->p->n = node;
  176. } else {
  177. list->first = node;
  178. }
  179. target->p = node;
  180. }
  181. void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  182. {
  183. node->p = target;
  184. node->n = target->n;
  185. if (target->n) {
  186. target->n->p = node;
  187. } else {
  188. list->last = node;
  189. }
  190. target->n = node;
  191. }
  192. void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node)
  193. {
  194. // remove from list
  195. if (node->p) {
  196. node->p->n = node->n;
  197. } else {
  198. list->first = node->n;
  199. }
  200. if (node->n) {
  201. node->n->p = node->p;
  202. } else {
  203. list->last = node->p;
  204. }
  205. }
  206. LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node)
  207. {
  208. return node->n;
  209. }
  210. LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node)
  211. {
  212. return node->p;
  213. }
  214. #endif