doubly_linked_list.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*!
  2. Temalia - Doubly linked list interface.
  3. Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia)
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef DOUBLYLINKEDLIST_
  18. #define DOUBLYLINKEDLIST_
  19. #include "platform.h"
  20. #include "iterator.h"
  21. struct _doubly_linked_list_t;
  22. typedef struct _doubly_linked_list_t *doubly_linked_list_t;
  23. /*!
  24. * @brief Returns a new doubly linked list.
  25. * Complexity O(1)
  26. */
  27. DECLSPEC doubly_linked_list_t doubly_linked_list_new(void);
  28. /*!
  29. * @brief Frees the memory occupied by doubly linked list.
  30. * Complexity O(n)
  31. *
  32. * @param Doubly linked list
  33. */
  34. DECLSPEC void doubly_linked_list_delete(doubly_linked_list_t list);
  35. /*!
  36. * @brief Checks if the list is empty.
  37. * Complexity O(1)
  38. *
  39. * @param Doubly linked list
  40. */
  41. DECLSPEC int doubly_linked_list_is_empty(doubly_linked_list_t list);
  42. /*!
  43. * @brief Returns first key from doubly linked list.
  44. * Complexity O(1)
  45. *
  46. * @param Doubly linked list
  47. */
  48. DECLSPEC void *doubly_linked_list_get_front(doubly_linked_list_t list);
  49. /*!
  50. * @brief Returns last key from doubly linked list.
  51. * Complexity O(1)
  52. *
  53. * @param Doubly linked list
  54. */
  55. DECLSPEC void *doubly_linked_list_get_back(doubly_linked_list_t list);
  56. /*!
  57. * @brief Returns iterator to first node of doubly linked list.
  58. * Complexity O(1)
  59. *
  60. * @param Doubly linked list
  61. */
  62. DECLSPEC doubly_linked_list_iterator_t doubly_linked_list_get_begin(
  63. doubly_linked_list_t list);
  64. /*!
  65. * @brief Sets list's "begin" iterator to given value.
  66. * Complexity O(1)
  67. *
  68. * @param Doubly linked list
  69. * @param New begin iterator
  70. */
  71. DECLSPEC void doubly_linked_list_set_begin(doubly_linked_list_t list,
  72. doubly_linked_list_iterator_t begin);
  73. /*!
  74. * @brief Returns iterator to last node of doubly linked list.
  75. * Complexity O(1)
  76. *
  77. * @param Doubly linked list
  78. */
  79. DECLSPEC doubly_linked_list_iterator_t doubly_linked_list_get_end(
  80. doubly_linked_list_t list);
  81. /*!
  82. * @brief Sets list's "end" iterator to given value.
  83. * Complexity O(1)
  84. *
  85. * @param Doubly linked list
  86. * @param New end iterator
  87. */
  88. DECLSPEC void doubly_linked_list_set_end(doubly_linked_list_t list,
  89. doubly_linked_list_iterator_t end);
  90. /*!
  91. * @brief Returns size of doubly linked list.
  92. * Complexity O(1)
  93. *
  94. * @param Doubly linked list
  95. */
  96. DECLSPEC int doubly_linked_list_get_size(doubly_linked_list_t list);
  97. /*!
  98. * @brief Sets the size of doubly linked list.
  99. * You may use this function in conjunction with linked_list_set_begin
  100. * and linked_list_set_end to clear the linked list.
  101. * Complexity O(1)
  102. *
  103. * @param Doubly linked list
  104. * @param New size
  105. */
  106. DECLSPEC void doubly_linked_list_set_size(doubly_linked_list_t list, int size);
  107. /*!
  108. * @brief Checks if iterator is part of doubly linked list.
  109. * Complexity O(n)
  110. *
  111. * @param Doubly linked list
  112. * @param Iterator
  113. */
  114. DECLSPEC int doubly_linked_list_check(doubly_linked_list_t list,
  115. doubly_linked_list_iterator_t it);
  116. /*!
  117. * @brief Returns iterator to a specified position in the doubly_linked_list.
  118. * Complexity O(n)
  119. *
  120. * @param Doubly linked list
  121. * @param Position
  122. */
  123. DECLSPEC doubly_linked_list_iterator_t doubly_linked_list_get_iterator_at(
  124. doubly_linked_list_t list, int position);
  125. /*!
  126. * @brief Returns the value from a position in doubly linked list.
  127. * Complexity O(n)
  128. *
  129. * @param Doubly linked list
  130. * @param Position
  131. */
  132. DECLSPEC void *doubly_linked_list_get_key_at(doubly_linked_list_t list, int position);
  133. /*!
  134. * @brief Changes the value from a linked list, given by it's position.
  135. * Complexity O(n)
  136. *
  137. * @param Doubly linked list
  138. * @param Position
  139. * @param New key reference
  140. */
  141. DECLSPEC void doubly_linked_list_set_key_at(doubly_linked_list_t list, int position,
  142. void *new_key);
  143. /*!
  144. * @brief Searches the doubly linked list for key and returns an iterator to it
  145. * if exists and NULL if not.
  146. * Complexity O(n)
  147. *
  148. * @param Doubly linked list
  149. * @param Key
  150. * @param Pointer to comparison function
  151. */
  152. DECLSPEC doubly_linked_list_iterator_t doubly_linked_list_search_key(
  153. doubly_linked_list_t list, void *key, int compare(void *x, void *y,
  154. void *context), void *context);
  155. /*!
  156. * @brief Returns the index/position of iterator in doubly linked list.
  157. * Complexity O(n)
  158. *
  159. * @param Doubly linked list
  160. * @param Iterator
  161. */
  162. DECLSPEC int doubly_linked_list_search_iterator(doubly_linked_list_t list,
  163. doubly_linked_list_iterator_t it);
  164. /*!
  165. * @brief Removes node from doubly linked list.
  166. * Complexity O(n)
  167. *
  168. * @param Doubly linked list
  169. * @param Iterator
  170. * @param Free the memory occupied by given iterator
  171. */
  172. DECLSPEC void doubly_linked_list_remove_iterator(doubly_linked_list_t list,
  173. doubly_linked_list_iterator_t it, int free);
  174. /*!
  175. * @brief Removes and returns first iterator containing given key in doubly linked list.
  176. * Complexity O(n)
  177. *
  178. * @param Doubly linked list
  179. * @param Key
  180. * @param Pointer to comparison function
  181. * @param Context
  182. * @param Free the iterator; if 1 then the function returns NULL
  183. */
  184. DECLSPEC doubly_linked_list_iterator_t doubly_linked_list_remove_key(
  185. doubly_linked_list_t list, void *key, int compare(void *x, void *y,
  186. void *context), void *context, int free);
  187. /*!
  188. * @brief Inserts key before an iterator in doubly linked list.
  189. * Complexity O(n)
  190. *
  191. * @param Doubly linked list
  192. * @param Iterator
  193. * @param Key
  194. */
  195. DECLSPEC void doubly_linked_list_insert_before(doubly_linked_list_t list,
  196. doubly_linked_list_iterator_t it, void *key);
  197. /*!
  198. * @brief Inserts key after an iterator in doubly linked list.
  199. * Complexity O(n)
  200. *
  201. * @param Doubly linked list
  202. * @param Iterator
  203. * @param Key
  204. */
  205. DECLSPEC void doubly_linked_list_insert_after(doubly_linked_list_t list,
  206. doubly_linked_list_iterator_t it, void *key);
  207. /*!
  208. * @brief Inserts key to beginning of doubly linked list.
  209. * Complexity O(1)
  210. *
  211. * @param Doubly linked list
  212. * @param Key
  213. */
  214. DECLSPEC void doubly_linked_list_push_front(doubly_linked_list_t list, void *key);
  215. /*!
  216. * @brief Inserts key to end of doubly linked list.
  217. * Complexity O(1)
  218. *
  219. * @param Doubly linked list
  220. * @param Key
  221. */
  222. DECLSPEC void doubly_linked_list_push_back(doubly_linked_list_t list, void *key);
  223. /*!
  224. * @brief Removes the first node of current list.
  225. * Complexity O(1)
  226. *
  227. * @param Doubly linked list
  228. */
  229. DECLSPEC void doubly_linked_list_pop_front(doubly_linked_list_t list);
  230. /*!
  231. * @brief Removes the last node of current list.
  232. * Complexity O(1)
  233. *
  234. * @param Doubly linked list.
  235. */
  236. DECLSPEC void doubly_linked_list_pop_back(doubly_linked_list_t list);
  237. /*!
  238. * @brief Prints keys of doubly linked list in two orders :
  239. * a) 1 - begin to end;
  240. * b) -1 - end to begin;
  241. * Complexity O(n)
  242. *
  243. * @param Doubly linked list
  244. * @param Iterating function
  245. * @param Context
  246. * @param Order value
  247. */
  248. DECLSPEC void doubly_linked_list_iterate(doubly_linked_list_t list, void key_handler(
  249. void *x, void *context), void *context, char order);
  250. /*!
  251. * @brief Sorts, using bubble sort, a doubly linked list.
  252. * Complexity O(n*n)
  253. *
  254. * @param Doubly linked list
  255. * @param Pointer to comparison function
  256. */
  257. DECLSPEC void doubly_linked_list_sort(doubly_linked_list_t list, int compare(void *x,
  258. void *y, void *context), void *context);
  259. /*!
  260. * @brief Merges two sorted doubly linked lists and returns the result.
  261. * Complexity O(n2)
  262. *
  263. * @param First doubly linked list
  264. * @param Second doubly linked list
  265. * @param Pointer to comparison function
  266. */
  267. DECLSPEC doubly_linked_list_t doubly_linked_list_merge(doubly_linked_list_t list1,
  268. doubly_linked_list_t list2,
  269. int compare(void *x, void *y, void *context), void *context);
  270. /*!
  271. * @brief Reverses INLINE (no additional memory needed) the keys from doubly linked list.
  272. * Complexity O(n)
  273. *
  274. * @param Doubly linked list
  275. */
  276. DECLSPEC void doubly_linked_list_reverse(doubly_linked_list_t list);
  277. /*!
  278. * @brief Inserts after iterator, of doubly linked list list1,
  279. * all keys from doubly linked list list2; list2 is destroyed !
  280. * Complexity O(1)
  281. *
  282. * @param First doubly linked list
  283. * @param Second doubly linked list
  284. * @param Iterator
  285. */
  286. DECLSPEC void doubly_linked_list_spice(doubly_linked_list_t list1,
  287. doubly_linked_list_t list2, doubly_linked_list_iterator_t it);
  288. #endif /* DOUBLYLINKEDLIST_ */