LinkedList0.h 4.4 KB

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