fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. #include "dllist.h"
  5.  
  6. // Function that compares two objects. Returns 1 if equal, 0 if not.
  7. int
  8. comparator(void *a, void *b)
  9. {
  10. if((int *)a == (void *)b)
  11. return 1;
  12. else;
  13. return 0;
  14. }
  15.  
  16. //Function that print a object
  17. int
  18. print(void *a)
  19. {
  20. printf("%d\n", *((int *)a));
  21. }
  22.  
  23. //Function that free object
  24. int
  25. free_o(void *a)
  26. {
  27. free(a);
  28. }
  29.  
  30. int
  31. main()
  32. {
  33. printf("This is a demo to show how to use dllist header.\n");
  34. printf("---------------------\n");
  35. printf("We add 1,2,3 int ponters to the list\n");
  36.  
  37. dllist *list;
  38. int a=1,b=2,c=3,d=4;
  39. dlnode *node;
  40.  
  41. list = list_new(&comparator, &print, &free_o);
  42.  
  43. list_add(list, &a);
  44. list_add(list, &b);
  45. list_add(list, &c);
  46.  
  47. list_print(list);
  48.  
  49. printf("Lets add one element at the end of the list.\n");
  50. list_add_end(list, &d);
  51. list_print(list);
  52.  
  53. printf("Now we will search for element with number 2.\n");
  54. node = list_search(list, &b);
  55. print(node->data);
  56.  
  57. printf("With the node we retrived from search, lets remove it from the list.\n");
  58. list_delnode(list, node);
  59. list_print(list);
  60.  
  61. printf("At next, we will pop the first element from the list and print the content.\n");
  62. node = list_pop(list);
  63. list_print(list);
  64.  
  65. return 0;
  66. }
  67.  
  68. #ifndef list_h
  69. #define list_h
  70.  
  71. typedef int (*dl_comparator)(void *, void *);
  72. typedef int (*dl_print)(void *);
  73. typedef int (*dl_freefunc)(void *);
  74.  
  75. typedef struct dlnode
  76. {
  77. struct dlnode *next;
  78. struct dlnode *prev;
  79. void *data;
  80. }dlnode;
  81.  
  82. typedef struct dllist
  83. {
  84. dlnode *start;
  85. dlnode *end;
  86. dl_comparator comp;
  87. dl_print print;
  88. dl_freefunc free_func;
  89. }dllist;
  90.  
  91. dllist *list_new(dl_comparator, dl_print, dl_freefunc);
  92. int list_add(dllist *, void *);
  93. int list_add_end(dllist *, void *);
  94. dlnode *list_search(dllist *, void *);
  95. int list_remove(dllist *, void *);
  96. void *list_pop(dllist *);
  97. void *list_pop_end(dllist *);
  98. void list_free(dllist *);
  99. void list_print(dllist *);
  100.  
  101. void *list_delnode(dllist *, dlnode *);
  102. int list_addafternode(dlnode *, void *);
  103. int list_addbeforenode(dlnode *, void *);
  104. #endif
  105.  
  106. #include<stdio.h>
  107. #include<stdlib.h>
  108.  
  109. #include "dllist.h"
  110.  
  111. dllist *
  112. list_new(dl_comparator comp, dl_print print, dl_freefunc ff)
  113. {
  114. dllist *list;
  115.  
  116. if(comp && print && ff)
  117. {
  118. list = malloc(sizeof(dllist));
  119. list->comp = comp;
  120. list->print = print;
  121. list->free_func = ff;
  122. list->start = NULL;
  123. list->end = NULL;
  124.  
  125. return list;
  126. }
  127. return NULL;
  128. }
  129.  
  130. int
  131. list_add(dllist *list, void *data)
  132. {
  133. dlnode *node;
  134. dlnode *tmp;
  135.  
  136. if(!(list && data))
  137. return 0;
  138.  
  139. node = malloc(sizeof(dlnode));
  140. if(node == NULL)
  141. return 0;
  142. node->data = data;
  143.  
  144. if(list->start == NULL)
  145. {
  146. list->start = node;
  147. list->end = node;
  148. node->next = NULL;
  149. node->prev = NULL;
  150. return 1;
  151. }
  152. tmp = list->start;
  153. list->start = node;
  154. node->next = tmp;
  155. node->prev = NULL;
  156. tmp->prev = node;
  157.  
  158. return 1;
  159. }
  160.  
  161. int
  162. list_add_end(dllist *list, void *data)
  163. {
  164. dlnode *node;
  165. dlnode *tmp;
  166.  
  167. if(!(list && data))
  168. return 0;
  169.  
  170. node = malloc(sizeof(dlnode));
  171. if(node == NULL)
  172. return 0;
  173. node->data = data;
  174.  
  175. if(list->start == NULL)
  176. {
  177. list->start = node;
  178. list->end = node;
  179. node->next = NULL;
  180. node->prev = NULL;
  181. return 1;
  182. }
  183. tmp = list->end;
  184. list->end= node;
  185. node->next = NULL;
  186. node->prev = tmp;
  187. tmp->next = node;
  188.  
  189. return 1;
  190. }
  191.  
  192. dlnode *
  193. list_search(dllist *list, void *data)
  194. {
  195. dlnode *node;
  196.  
  197. if(!list)
  198. return NULL;
  199. if(!(list->start))
  200. return NULL;
  201. if(!data)
  202. return NULL;
  203.  
  204. node = list->start;
  205. while(node != NULL)
  206. {
  207. if(list->comp(data, node->data))
  208. return node;
  209. node = node->next;
  210. }
  211. return NULL;
  212. }
  213.  
  214. int
  215. list_remove(dllist *list, void *data)
  216. {
  217. dlnode *node;
  218. if(!list)
  219. return 0;
  220. if(!(list->start))
  221. return 0;
  222. if(!data)
  223. return 0;
  224.  
  225. node = list->start;
  226. while(node != NULL)
  227. {
  228. if(list->comp(data, node->data))
  229. {
  230. if(node->prev)
  231. {
  232. if(node->next)
  233. node->prev->next = node->next;
  234. else
  235. node->prev->next = NULL;
  236. }
  237. if(node->next)
  238. {
  239. if(node->prev)
  240. node->next->prev = node->prev;
  241. else
  242. node->next->prev = NULL;
  243. }
  244.  
  245. if(list->start == node)
  246. {
  247. list->start = node->next;
  248. if(!(node->next))
  249. list->end = node->next;
  250. }
  251. else if(list->end == node)
  252. {
  253. list->end = NULL;
  254. list->start = NULL;
  255. }
  256.  
  257. list->free_func(node->data);
  258. free(node);
  259. node = NULL;
  260.  
  261. return 1;
  262. }
  263. node = node->next;
  264. }
  265. return 0;
  266. }
  267.  
  268. void *
  269. list_pop(dllist *list)
  270. {
  271. dlnode *node;
  272. void *data;
  273. node = list->start;
  274. node->next->prev = NULL;
  275. data = node->data;
  276. list->start = list->start->next;
  277. free(node);
  278. node = NULL;
  279. return data;
  280. }
  281.  
  282. void *
  283. list_pop_end(dllist *list)
  284. {
  285. dlnode *node;
  286. void *data;
  287. node = list->end;
  288. node->prev->next = NULL;
  289. data = node->data;
  290. list->end = node->prev;
  291. free(node);
  292. node = NULL;
  293. return data;
  294. }
  295.  
  296. void
  297. list_free(dllist *list)
  298. {
  299. dlnode *node;
  300. dlnode *tmp;
  301. node = list->start;
  302. while(node)
  303. {
  304. list->free_func(node->data);
  305. tmp = node;
  306. node = node->next;
  307. free(tmp);
  308. tmp = NULL;
  309. }
  310. free(list);
  311. list = NULL;
  312. }
  313.  
  314. void *
  315. list_delnode(dllist *list, dlnode *node)
  316. {
  317. void *data;
  318.  
  319. if(!node)
  320. return NULL;
  321.  
  322. if(node->prev)
  323. {
  324. if(node->next)
  325. node->prev->next = node->next;
  326. else
  327. node->prev->next = NULL;
  328. }
  329. if(node->next)
  330. {
  331. if(node->prev)
  332. node->next->prev = node->prev;
  333. else
  334. node->next->prev = NULL;
  335. }
  336.  
  337. if(list->start == node)
  338. {
  339. list->start = node->next;
  340. if(!(node->next))
  341. list->end = node->next;
  342. }
  343. else if(list->end == node)
  344. {
  345. list->end = NULL;
  346. list->start = NULL;
  347. }
  348.  
  349. data = node->data;
  350. free(node);
  351. node = NULL;
  352. return data;
  353. }
  354.  
  355. int
  356. list_addafternode(dlnode *node, void *data)
  357. {
  358. dlnode *new_node;
  359. new_node = malloc(sizeof(new_node));
  360. if(!new_node)
  361. return 0;
  362. new_node->data = data;
  363. new_node->next = node->next;
  364. new_node->prev = node;
  365. node->next = new_node;
  366. return 1;
  367. }
  368.  
  369. int
  370. list_addbeforenode(dlnode *node, void *data)
  371. {
  372. dlnode *new_node;
  373. new_node = malloc(sizeof(new_node));
  374. if(!new_node)
  375. return 0;
  376. new_node->data = data;
  377. new_node->next = node->next;
  378. new_node->prev = node;
  379. node->next = new_node;
  380. return 1;
  381. }
  382.  
  383. void
  384. list_print(dllist *list)
  385. {
  386. int i = 0;
  387. dlnode *node;
  388.  
  389. if(list)
  390. if(list->start)
  391. node = list->start;
  392. else
  393. return;
  394. else
  395. return;
  396.  
  397. while(node)
  398. {
  399. printf("%d: ",i);
  400. list->print(node->data);
  401. node = node->next;
  402. i++;
  403. }
  404. return;
  405. }
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