fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #include "dllist.h"
  5.  
  6. dllist *
  7. list_new(dl_comparator comp, dl_print print, dl_freefunc ff)
  8. {
  9. dllist *list;
  10.  
  11. if(comp && print && ff)
  12. {
  13. list = malloc(sizeof(dllist));
  14. list->comp = comp;
  15. list->print = print;
  16. list->free_func = ff;
  17. list->start = NULL;
  18. list->end = NULL;
  19.  
  20. return list;
  21. }
  22. return NULL;
  23. }
  24.  
  25. int
  26. list_add(dllist *list, void *data)
  27. {
  28. dlnode *node;
  29. dlnode *tmp;
  30.  
  31. if(!(list && data))
  32. return 0;
  33.  
  34. node = malloc(sizeof(dlnode));
  35. if(node == NULL)
  36. return 0;
  37. node->data = data;
  38.  
  39. if(list->start == NULL)
  40. {
  41. list->start = node;
  42. list->end = node;
  43. node->next = NULL;
  44. node->prev = NULL;
  45. return 1;
  46. }
  47. tmp = list->start;
  48. list->start = node;
  49. node->next = tmp;
  50. node->prev = NULL;
  51. tmp->prev = node;
  52.  
  53. return 1;
  54. }
  55.  
  56. int
  57. list_add_end(dllist *list, void *data)
  58. {
  59. dlnode *node;
  60. dlnode *tmp;
  61.  
  62. if(!(list && data))
  63. return 0;
  64.  
  65. node = malloc(sizeof(dlnode));
  66. if(node == NULL)
  67. return 0;
  68. node->data = data;
  69.  
  70. if(list->start == NULL)
  71. {
  72. list->start = node;
  73. list->end = node;
  74. node->next = NULL;
  75. node->prev = NULL;
  76. return 1;
  77. }
  78. tmp = list->end;
  79. list->end= node;
  80. node->next = NULL;
  81. node->prev = tmp;
  82. tmp->next = node;
  83.  
  84. return 1;
  85. }
  86.  
  87. dlnode *
  88. list_search(dllist *list, void *data)
  89. {
  90. dlnode *node;
  91.  
  92. if(!list)
  93. return NULL;
  94. if(!(list->start))
  95. return NULL;
  96. if(!data)
  97. return NULL;
  98.  
  99. node = list->start;
  100. while(node != NULL)
  101. {
  102. if(list->comp(data, node->data))
  103. return node;
  104. node = node->next;
  105. }
  106. return NULL;
  107. }
  108.  
  109. int
  110. list_remove(dllist *list, void *data)
  111. {
  112. dlnode *node;
  113. if(!list)
  114. return 0;
  115. if(!(list->start))
  116. return 0;
  117. if(!data)
  118. return 0;
  119.  
  120. node = list->start;
  121. while(node != NULL)
  122. {
  123. if(list->comp(data, node->data))
  124. {
  125. if(node->prev)
  126. {
  127. if(node->next)
  128. node->prev->next = node->next;
  129. else
  130. node->prev->next = NULL;
  131. }
  132. if(node->next)
  133. {
  134. if(node->prev)
  135. node->next->prev = node->prev;
  136. else
  137. node->next->prev = NULL;
  138. }
  139.  
  140. if(list->start == node)
  141. {
  142. list->start = node->next;
  143. if(!(node->next))
  144. list->end = node->next;
  145. }
  146. else if(list->end == node)
  147. {
  148. list->end = NULL;
  149. list->start = NULL;
  150. }
  151.  
  152. list->free_func(node->data);
  153. free(node);
  154. node = NULL;
  155.  
  156. return 1;
  157. }
  158. node = node->next;
  159. }
  160. return 0;
  161. }
  162.  
  163. void *
  164. list_pop(dllist *list)
  165. {
  166. dlnode *node;
  167. void *data;
  168. node = list->start;
  169. node->next->prev = NULL;
  170. data = node->data;
  171. list->start = list->start->next;
  172. free(node);
  173. node = NULL;
  174. return data;
  175. }
  176.  
  177. void *
  178. list_pop_end(dllist *list)
  179. {
  180. dlnode *node;
  181. void *data;
  182. node = list->end;
  183. node->prev->next = NULL;
  184. data = node->data;
  185. list->end = node->prev;
  186. free(node);
  187. node = NULL;
  188. return data;
  189. }
  190.  
  191. void
  192. list_free(dllist *list)
  193. {
  194. dlnode *node;
  195. dlnode *tmp;
  196. node = list->start;
  197. while(node)
  198. {
  199. list->free_func(node->data);
  200. tmp = node;
  201. node = node->next;
  202. free(tmp);
  203. tmp = NULL;
  204. }
  205. free(list);
  206. list = NULL;
  207. }
  208.  
  209. void *
  210. list_delnode(dllist *list, dlnode *node)
  211. {
  212. void *data;
  213.  
  214. if(!node)
  215. return NULL;
  216.  
  217. if(node->prev)
  218. {
  219. if(node->next)
  220. node->prev->next = node->next;
  221. else
  222. node->prev->next = NULL;
  223. }
  224. if(node->next)
  225. {
  226. if(node->prev)
  227. node->next->prev = node->prev;
  228. else
  229. node->next->prev = NULL;
  230. }
  231.  
  232. if(list->start == node)
  233. {
  234. list->start = node->next;
  235. if(!(node->next))
  236. list->end = node->next;
  237. }
  238. else if(list->end == node)
  239. {
  240. list->end = NULL;
  241. list->start = NULL;
  242. }
  243.  
  244. data = node->data;
  245. free(node);
  246. node = NULL;
  247. return data;
  248. }
  249.  
  250. int
  251. list_addafternode(dlnode *node, void *data)
  252. {
  253. dlnode *new_node;
  254. new_node = malloc(sizeof(new_node));
  255. if(!new_node)
  256. return 0;
  257. new_node->data = data;
  258. new_node->next = node->next;
  259. new_node->prev = node;
  260. node->next = new_node;
  261. return 1;
  262. }
  263.  
  264. int
  265. list_addbeforenode(dlnode *node, void *data)
  266. {
  267. dlnode *new_node;
  268. new_node = malloc(sizeof(new_node));
  269. if(!new_node)
  270. return 0;
  271. new_node->data = data;
  272. new_node->next = node->next;
  273. new_node->prev = node;
  274. node->next = new_node;
  275. return 1;
  276. }
  277.  
  278. void
  279. list_print(dllist *list)
  280. {
  281. int i = 0;
  282. dlnode *node;
  283.  
  284. if(list)
  285. if(list->start)
  286. node = list->start;
  287. else
  288. return;
  289. else
  290. return;
  291.  
  292. while(node)
  293. {
  294. printf("%d: ",i);
  295. list->print(node->data);
  296. node = node->next;
  297. i++;
  298. }
  299. return;
  300. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:4:20: fatal error: dllist.h: No such file or directory
 #include "dllist.h"
                    ^
compilation terminated.
stdout
Standard output is empty