LinkedList1.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @file LinkedList1.h
  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. * @section DESCRIPTION
  23. *
  24. * Simple doubly linked list.
  25. */
  26. #ifndef BADVPN_STRUCTURE_LINKEDLIST1_H
  27. #define BADVPN_STRUCTURE_LINKEDLIST1_H
  28. #include <stddef.h>
  29. #include <misc/debug.h>
  30. /**
  31. * Linked list node.
  32. */
  33. typedef struct LinkedList1Node_t
  34. {
  35. struct LinkedList1Node_t *p;
  36. struct LinkedList1Node_t *n;
  37. } LinkedList1Node;
  38. /**
  39. * Simple doubly linked list.
  40. */
  41. typedef struct
  42. {
  43. LinkedList1Node *first;
  44. LinkedList1Node *last;
  45. } LinkedList1;
  46. /**
  47. * Initializes the linked list.
  48. *
  49. * @param list list to initialize
  50. */
  51. static void LinkedList1_Init (LinkedList1 *list);
  52. /**
  53. * Determines if the list is empty.
  54. *
  55. * @param list the list
  56. * @return 1 if empty, 0 if not
  57. */
  58. static int LinkedList1_IsEmpty (LinkedList1 *list);
  59. /**
  60. * Returns the first node of the list.
  61. *
  62. * @param list the list
  63. * @return first node of the list, or NULL if the list is empty
  64. */
  65. static LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list);
  66. /**
  67. * Returns the last node of the list.
  68. *
  69. * @param list the list
  70. * @return last node of the list, or NULL if the list is empty
  71. */
  72. static LinkedList1Node * LinkedList1_GetLast (LinkedList1 *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 LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node);
  80. /**
  81. * Inserts a node to the end of the list.
  82. *
  83. * @param list the list
  84. * @param node uninitialized node to insert
  85. */
  86. static void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node);
  87. /**
  88. * Inserts a node before a given node.
  89. *
  90. * @param list the list
  91. * @param node uninitialized node to insert
  92. * @param target node in the list to insert before
  93. */
  94. static void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
  95. /**
  96. * Inserts a node after a given node.
  97. *
  98. * @param list the list
  99. * @param node uninitialized node to insert
  100. * @param target node in the list to insert after
  101. */
  102. static void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target);
  103. /**
  104. * Removes a node from the list.
  105. *
  106. * @param list the list
  107. * @param node node to remove
  108. */
  109. static void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node);
  110. /**
  111. * Returns the next node of a given node.
  112. *
  113. * @param node reference node
  114. * @return next node, or NULL if none
  115. */
  116. static LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node);
  117. /**
  118. * Returns the previous node of a given node.
  119. *
  120. * @param node reference node
  121. * @return previous node, or NULL if none
  122. */
  123. static LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node);
  124. void LinkedList1_Init (LinkedList1 *list)
  125. {
  126. list->first = NULL;
  127. list->last = NULL;
  128. }
  129. int LinkedList1_IsEmpty (LinkedList1 *list)
  130. {
  131. return (!list->first);
  132. }
  133. LinkedList1Node * LinkedList1_GetFirst (LinkedList1 *list)
  134. {
  135. return (list->first);
  136. }
  137. LinkedList1Node * LinkedList1_GetLast (LinkedList1 *list)
  138. {
  139. return (list->last);
  140. }
  141. void LinkedList1_Prepend (LinkedList1 *list, LinkedList1Node *node)
  142. {
  143. node->p = NULL;
  144. node->n = list->first;
  145. if (list->first) {
  146. list->first->p = node;
  147. } else {
  148. list->last = node;
  149. }
  150. list->first = node;
  151. }
  152. void LinkedList1_Append (LinkedList1 *list, LinkedList1Node *node)
  153. {
  154. node->p = list->last;
  155. node->n = NULL;
  156. if (list->last) {
  157. list->last->n = node;
  158. } else {
  159. list->first = node;
  160. }
  161. list->last = node;
  162. }
  163. void LinkedList1_InsertBefore (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  164. {
  165. node->p = target->p;
  166. node->n = target;
  167. if (target->p) {
  168. target->p->n = node;
  169. } else {
  170. list->first = node;
  171. }
  172. target->p = node;
  173. }
  174. void LinkedList1_InsertAfter (LinkedList1 *list, LinkedList1Node *node, LinkedList1Node *target)
  175. {
  176. node->p = target;
  177. node->n = target->n;
  178. if (target->n) {
  179. target->n->p = node;
  180. } else {
  181. list->last = node;
  182. }
  183. target->n = node;
  184. }
  185. void LinkedList1_Remove (LinkedList1 *list, LinkedList1Node *node)
  186. {
  187. // remove from list
  188. if (node->p) {
  189. node->p->n = node->n;
  190. } else {
  191. list->first = node->n;
  192. }
  193. if (node->n) {
  194. node->n->p = node->p;
  195. } else {
  196. list->last = node->p;
  197. }
  198. }
  199. LinkedList1Node * LinkedList1Node_Next (LinkedList1Node *node)
  200. {
  201. return node->n;
  202. }
  203. LinkedList1Node * LinkedList1Node_Prev (LinkedList1Node *node)
  204. {
  205. return node->p;
  206. }
  207. #endif