linked_list.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. Temelia - 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 LINKEDLIST_H_
  18. #define LINKEDLIST_H_
  19. #include "iterator.h"
  20. typedef struct _linked_list_t *linked_list_t;
  21. /*!
  22. * @brief Constructor - returns a new linked list.
  23. * Complexity O(1)
  24. */
  25. DECLSPEC linked_list_t linked_list_new(void);
  26. /*!
  27. * @brief Frees the memory occupied by linked list;
  28. * all list's iterators become invalid.
  29. * Complexity O(n)
  30. *
  31. * @param Linked list
  32. */
  33. DECLSPEC void linked_list_delete(linked_list_t list);
  34. /*!
  35. * @brief Checks if the list is empty.
  36. * Complexity O(1)
  37. *
  38. * @param Linked list
  39. */
  40. DECLSPEC int linked_list_is_empty(linked_list_t list);
  41. /*!
  42. * @brief Returns the first key.
  43. * Complexity O(1)
  44. *
  45. * @param Linked list
  46. */
  47. DECLSPEC void *linked_list_get_front(linked_list_t list);
  48. /*!
  49. * @brief Returns the last key.
  50. * Complexity O(1)
  51. *
  52. * @param Linked list
  53. */
  54. DECLSPEC void *linked_list_get_back(linked_list_t list);
  55. /*!
  56. * @brief Returns iterator to first node of linked list.
  57. * Complexity O(1)
  58. *
  59. * @param Linked list
  60. */
  61. DECLSPEC linked_list_iterator_t linked_list_get_begin(linked_list_t list);
  62. /*!
  63. * @brief Sets list's "begin" iterator to given value.
  64. * Complexity O(1)
  65. *
  66. * @param Linked list
  67. * @param New begin iterator
  68. */
  69. DECLSPEC void linked_list_set_begin(linked_list_t list, linked_list_iterator_t begin);
  70. /*!
  71. * @brief Returns iterator to last node.
  72. * Complexity O(1)
  73. *
  74. * @param Linked list
  75. */
  76. DECLSPEC linked_list_iterator_t linked_list_get_end(linked_list_t list);
  77. /*!
  78. * @brief Sets list's "end" iterator to given value.
  79. * Complexity O(1)
  80. *
  81. * @param Linked list
  82. * @param New end iterator
  83. */
  84. DECLSPEC void linked_list_set_end(linked_list_t list, linked_list_iterator_t end);
  85. /*!
  86. * @brief Returns the size of linked list.
  87. * Complexity O(1)
  88. *
  89. * @param Linked list
  90. */
  91. DECLSPEC int linked_list_get_size(linked_list_t list);
  92. /*!
  93. * @brief Sets the size of linked list.
  94. * You may use this function in conjunction with linked_list_set_begin
  95. * and linked_list_set_end to clear the linked list.
  96. * Complexity O(1)
  97. *
  98. * @param Linked list
  99. * @param New size
  100. */
  101. DECLSPEC void linked_list_set_size(linked_list_t list, int size);
  102. /*!
  103. * @brief Returns true if iterator is part of linked list.
  104. * Complexity O(n)
  105. *
  106. * @param Linked list
  107. * @param Iterator
  108. */
  109. DECLSPEC int linked_list_check(linked_list_t list, linked_list_iterator_t it);
  110. /*!
  111. * @brief Returns the previous node pointed by iterator;
  112. * the operation if expensive because linke_list_iterator
  113. * has no pointer to the previous iterator in the list.
  114. * Complexity O(n)
  115. *
  116. * @param Linked list
  117. * @param Iterator
  118. */
  119. DECLSPEC linked_list_iterator_t linked_list_iterator_get_prev(linked_list_t list,
  120. linked_list_iterator_t it);
  121. /*!
  122. * @brief Returns the iterator to specified position in linked list.
  123. * Complexity O(n)
  124. *
  125. * @param Linked list
  126. * @param Position
  127. */
  128. DECLSPEC linked_list_iterator_t linked_list_get_iterator_at(linked_list_t list,
  129. int position);
  130. /*!
  131. * @brief Returns the value from a position in linked list.
  132. * Complexity O(n)
  133. *
  134. * @param Linked list
  135. * @param Position
  136. */
  137. DECLSPEC void *linked_list_get_key_at(linked_list_t list, int position);
  138. /*!
  139. * @brief Changes the value from a linked list, given by it's position.
  140. * Complexity O(n)
  141. *
  142. * @param Linked list
  143. * @param Position
  144. * @param New key
  145. */
  146. DECLSPEC void linked_list_set_key_at(linked_list_t list, int position, void *new_key);
  147. /*!
  148. * @brief Searches over linked list for key and returns an iterator to the
  149. * first occurrence.
  150. * Complexity O(n)
  151. *
  152. * @param Linked list
  153. * @param Key
  154. * @param Pointer to comparison function
  155. */
  156. DECLSPEC linked_list_iterator_t linked_list_search_key(linked_list_t list, void *key,
  157. int compare(void *x, void *y, void *context), void *context);
  158. /*!
  159. * @brief Returns the index/position of iterator in linked list.
  160. * Complexity O(n)
  161. *
  162. * @param Linked list
  163. * @param Iterator
  164. */
  165. DECLSPEC int linked_list_search_iterator(linked_list_t list, linked_list_iterator_t it);
  166. /*!
  167. * @brief Removes iterator from linked list
  168. * Complexity O(n)
  169. *
  170. * @param Linked list
  171. * @param Iterator
  172. * @param Value on which freeing the iterator depends; 1 if you want to free the memory occupied
  173. * by the iterator and 0 if you don't
  174. */
  175. DECLSPEC void linked_list_remove_iterator(linked_list_t list, linked_list_iterator_t it,
  176. int free);
  177. /*!
  178. * @brief Removes and returns first node that contains key.
  179. * Complexity O(n)
  180. *
  181. * @param Linked list
  182. * @param Key
  183. * @param Pointer to comparison function
  184. * @param Free the iterator containing that key
  185. */
  186. DECLSPEC linked_list_iterator_t linked_list_remove_key(linked_list_t list, void *key,
  187. int compare(void *x, void *y, void *context), void *context, int free);
  188. /*!
  189. * @brief Inserts key after iterator in linked list
  190. * Complexity O(1)
  191. *
  192. * @param Linked list
  193. * @param Iterator
  194. * @param Key
  195. */
  196. DECLSPEC void linked_list_insert_after(linked_list_t list, linked_list_iterator_t it,
  197. void *key);
  198. /*!
  199. * @brief Inserts key before iterator , in linked list.
  200. * Complexity O(1)
  201. *
  202. * @param Linked list
  203. * @param Iterator
  204. * @param Key
  205. */
  206. DECLSPEC void linked_list_insert_before(linked_list_t list, linked_list_iterator_t it,
  207. void *key);
  208. /*!
  209. * @brief Inserts an key to beginning of linked list.
  210. * Complexity O(1)
  211. *
  212. * @param Linked list
  213. */
  214. DECLSPEC void linked_list_push_front(linked_list_t list, void *key);
  215. /*!
  216. * @brief Inserts an key to end of linked list.
  217. * Complexity O(1)
  218. *
  219. * @param Linked list
  220. * @param Key
  221. */
  222. DECLSPEC void linked_list_push_back(linked_list_t list, void *key);
  223. /*!
  224. * @brief Removes the first node of current list.
  225. * Complexity O(1)
  226. *
  227. * @param Linked list
  228. */
  229. DECLSPEC void linked_list_pop_front(linked_list_t list);
  230. /*!
  231. * @brief Removes the last node of current list.
  232. * Complexity O(1)
  233. *
  234. * @param Linked list
  235. */
  236. DECLSPEC void linked_list_pop_back(linked_list_t list);
  237. /*! @brief Iterates over the keys of current Linked list
  238. * a) 1 - begin to end
  239. * b) -1 - end to begin.
  240. * Complexity O(n)
  241. *
  242. * @param Linked list
  243. * @param Pointer to iterating function
  244. * @param Context
  245. * @param Order
  246. */
  247. DECLSPEC void linked_list_iterate(linked_list_t list, void key_handler(void *x,
  248. void *context), void *context, char order);
  249. /*!
  250. * @brief Sorts a linked list using the bubble sort algorithm
  251. * Complexity O(n*n)
  252. *
  253. * @param Linked list
  254. * @param Pointer to comparison function.
  255. */
  256. DECLSPEC void linked_list_sort(linked_list_t list, int compare(void *x, void *y,
  257. void *context), void *context);
  258. /*!
  259. * @brief Merges two linked lists and returns the result : (list1, list2).
  260. * Complexity O(n1 + n2)
  261. *
  262. * @param First linked list
  263. * @param Second linked list
  264. * @param Pointer to comparison function
  265. */
  266. DECLSPEC linked_list_t linked_list_merge(linked_list_t list1, linked_list_t list2,
  267. int compare(void *x, void *y, void *context), void *context);
  268. /*!
  269. * @brief Reverses the keys from a linked list; the reverse is done "INLINE",
  270. * without any additional memory.
  271. * Complexity O(n)
  272. *
  273. * @param Linked list
  274. */
  275. DECLSPEC void linked_list_reverse(linked_list_t list);
  276. /*!
  277. * @brief Inserts the iterator from linked list "list2" after
  278. * the iterator "it1" of linked list "list1". The second
  279. * linked list will become invalid: all it's iterators
  280. * will be part of the first linked list.
  281. * Complexity O(1)
  282. *
  283. * @param First linked list
  284. * @param Second linked list
  285. * @param Iterator; NULL if you want to unsorted merge the lists
  286. */
  287. DECLSPEC void linked_list_spice(linked_list_t list1, linked_list_t list2,
  288. linked_list_iterator_t it1);
  289. #endif /* LINKED_LIST_H */