diff options
| author | simon MAGNIN-FEYSOT | 2018-01-17 19:25:08 +0100 |
|---|---|---|
| committer | jvoisin | 2018-01-17 19:25:08 +0100 |
| commit | b213c8e08b5624da9bb69dd05e132e247bab3082 (patch) | |
| tree | 70098ca8ea967138d4beb3a1f423e5beb2908fd3 /src/sp_list.c | |
| parent | 05e4b7ea67e07bb82db2a37930b383d51500b341 (diff) | |
Remove useless "head" member in our linked lists implementation
This should close #85
Diffstat (limited to 'src/sp_list.c')
| -rw-r--r-- | src/sp_list.c | 47 |
1 files changed, 14 insertions, 33 deletions
diff --git a/src/sp_list.c b/src/sp_list.c index 447f479..8879d89 100644 --- a/src/sp_list.c +++ b/src/sp_list.c | |||
| @@ -11,12 +11,6 @@ void sp_list_free(sp_list_node *node) { | |||
| 11 | } | 11 | } |
| 12 | } | 12 | } |
| 13 | 13 | ||
| 14 | sp_list_node *sp_list_new() { | ||
| 15 | sp_list_node *new = pecalloc(sizeof(*new), 1, 1); | ||
| 16 | new->next = new->data = new->head = NULL; | ||
| 17 | return new; | ||
| 18 | } | ||
| 19 | |||
| 20 | // Thanks to https://en.wikipedia.org/wiki/Insertion_sort :> | 14 | // Thanks to https://en.wikipedia.org/wiki/Insertion_sort :> |
| 21 | sp_list_node *sp_list_sort(sp_list_node *pList, | 15 | sp_list_node *sp_list_sort(sp_list_node *pList, |
| 22 | int (*cmp_func)(sp_list_node *, sp_list_node *)) { | 16 | int (*cmp_func)(sp_list_node *, sp_list_node *)) { |
| @@ -46,39 +40,26 @@ sp_list_node *sp_list_sort(sp_list_node *pList, | |||
| 46 | return head; | 40 | return head; |
| 47 | } | 41 | } |
| 48 | 42 | ||
| 49 | void sp_list_insert(sp_list_node *list, void *data) { | 43 | sp_list_node *sp_list_insert(sp_list_node *list, void *data) { |
| 50 | if (list->head == NULL) { | 44 | sp_list_node *new = pecalloc(sizeof(*new), 1, 1); |
| 51 | list->data = data; | 45 | sp_list_node *origin = list; |
| 52 | list->next = NULL; | 46 | new->data = data; |
| 53 | list->head = list; | 47 | new->next = NULL; |
| 54 | } else { | ||
| 55 | sp_list_node *new = pecalloc(sizeof(*new), 1, 1); | ||
| 56 | |||
| 57 | new->data = data; | ||
| 58 | new->next = NULL; | ||
| 59 | new->head = list; | ||
| 60 | 48 | ||
| 49 | if (list == NULL) { | ||
| 50 | origin = new; | ||
| 51 | } else { | ||
| 61 | while (list->next) { | 52 | while (list->next) { |
| 62 | list = list->next; | 53 | list = list->next; |
| 63 | } | 54 | } |
| 64 | list->next = new; | 55 | list->next = new; |
| 65 | } | 56 | } |
| 57 | return origin; | ||
| 66 | } | 58 | } |
| 67 | 59 | ||
| 68 | void sp_list_prepend(sp_list_node *list, void *data) { | 60 | sp_list_node *sp_list_prepend(sp_list_node *list, void *data) { |
| 69 | if (list->head == NULL) { | 61 | sp_list_node *new = pecalloc(sizeof(*new), 1, 1); |
| 70 | list->data = data; | 62 | new->next = list; |
| 71 | list->next = NULL; | 63 | new->data = data; |
| 72 | list->head = list; | 64 | return new; |
| 73 | } else { | ||
| 74 | sp_list_node *new = pecalloc(sizeof(*new), 1, 1); | ||
| 75 | |||
| 76 | new->next = list->next; | ||
| 77 | list->next = new; | ||
| 78 | |||
| 79 | new->head = list; | ||
| 80 | |||
| 81 | new->data = list->data; | ||
| 82 | list->data = data; | ||
| 83 | } | ||
| 84 | } | 65 | } |
