Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. #ifndef LINKED_LIST_H_INCLUDED
  2. #define LINKED_LIST_H_INCLUDED
  3.  
  4. #define list_size(list) (list->size)
  5.  
  6. #define list_head(list) (list->head)
  7. #define list_tail(list) (list->tail)
  8. #define list_is_head(list, element) (element == list_head(list))
  9. #define list_is_tail(list, element) (element == list_tail(list))
  10.  
  11. #define list_append(list, data) list_ins_next(list, list->tail, data)
  12. #define list_preprend(list, data) list_ins_next(list, NULL, data)
  13.  
  14. #define list_data(element) (element->data)
  15. #define list_next(element) (element->nextElmt)
  16.  
  17. typedef struct ListElmt_{
  18. void *data;
  19. struct ListElmt_ *nextElmt;
  20. } ListElmt;
  21.  
  22. typedef struct List_{
  23. ListElmt *head;
  24. ListElmt *tail;
  25. size_t size;
  26. void (*destroy)(void *data);
  27. int (*cmp)(const void *fdata,const void *sdata);
  28. }List;
  29.  
  30.  
  31. List *list_init(void (*destroy)(void *data),
  32. int (*cmp)(const void *fdata, const void *sdata));
  33. void list_destroy(List *list);
  34. int list_ins_off(List *list, size_t off, void *data);
  35. int list_ins_next(List *list, ListElmt *element, void *data);
  36. int list_drem_next(List *list, ListElmt *element);
  37. int list_rem_next(List *list, ListElmt *element, void ** data);
  38. ListElmt * list_find(List *list, void *data, ListElmt *sElmt);
  39.  
  40. #endif
  41.  
  42. #include <stdlib.h>
  43.  
  44. #include "linked_list.h"
  45.  
  46.  
  47. List * list_init(void (*destroy)(void *data),
  48. int (*cmp)(const void *fdata, const void *data))
  49. {
  50. List *rlist;
  51.  
  52. if((rlist = malloc(sizeof *rlist)) != NULL) {
  53. rlist->head = NULL;
  54. rlist->tail = NULL;
  55. rlist->size = 0;
  56. rlist->destroy = destroy;
  57. rlist->cmp = cmp;
  58. }
  59.  
  60. return rlist;
  61. }
  62.  
  63. void list_destroy(List *list) {
  64.  
  65. ListElmt * el;
  66. void *data;
  67.  
  68. while( (el=list_head(list)) != NULL) {
  69. list_rem_next(list, NULL, &data);
  70. list->destroy(data);
  71. }
  72.  
  73. free(list);
  74. }
  75.  
  76. int list_ins_next(List *list, ListElmt *elmt, void *data) {
  77.  
  78. ListElmt *addElmt = malloc(sizeof *addElmt);
  79. if(addElmt == NULL)
  80. return -1;
  81.  
  82. addElmt->data = data;
  83.  
  84. if(elmt == NULL) {
  85. addElmt->nextElmt = list->head;
  86. elmt = list->head = addElmt; // using elmt for checking list tail
  87. }
  88. else {
  89. addElmt->nextElmt = elmt->nextElmt;
  90. elmt->nextElmt = addElmt;
  91. }
  92.  
  93. // updating list->tail if needed
  94. if(elmt == list->tail)
  95. list->tail = addElmt;
  96.  
  97. list->size++;
  98.  
  99. return 0;
  100. }
  101.  
  102. int list_ins_off(List *list, size_t off, void *data) {
  103. if(off < 0 || off >= list->size)
  104. return list_append(list, data);
  105.  
  106. size_t i;
  107. ListElmt *elmt = NULL;
  108.  
  109. for(i = 0; i < off; ++i)
  110. elmt = elmt->nextElmt;
  111.  
  112. list_ins_next(list, elmt, data);
  113. return 0;
  114. }
  115.  
  116. int list_drem_next(List *list, ListElmt *element) {
  117. void *data;
  118. int ret = list_rem_next(list, element, &data);
  119.  
  120. if(ret != 0)
  121. return ret;
  122.  
  123. list->destroy(data);
  124. return 0;
  125. }
  126.  
  127. int list_rem_next(List *list, ListElmt *element, void **data) {
  128. ListElmt *remElmt;
  129.  
  130. // the list still not contain any element
  131. if(list->size == 0 || element->nextElmt == NULL)
  132. return -1;
  133.  
  134. *data = element->nextElmt->data;
  135. remElmt = element->nextElmt;
  136.  
  137. element->nextElmt = element->nextElmt->nextElmt;
  138.  
  139. if(remElmt == list->tail)
  140. list->tail = element;
  141.  
  142. free(remElmt);
  143. list->size--;
  144.  
  145. return 0;
  146. }
  147.  
  148. ListElmt * find(List *list, void *data, ListElmt *sElmt) {
  149. if(list->size == 0 || list->cmp == NULL)
  150. return NULL;
  151.  
  152. sElmt = sElmt ? sElmt : list_head(list);
  153.  
  154. while(sElmt && list->cmp(sElmt->data, data))
  155. sElmt = sElmt->nextElmt;
  156.  
  157. return sElmt;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement