From b213c8e08b5624da9bb69dd05e132e247bab3082 Mon Sep 17 00:00:00 2001 From: simon MAGNIN-FEYSOT Date: Wed, 17 Jan 2018 19:25:08 +0100 Subject: Remove useless "head" member in our linked lists implementation This should close #85 --- src/sp_list.c | 47 ++++++++++++++--------------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) (limited to 'src/sp_list.c') 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) { } } -sp_list_node *sp_list_new() { - sp_list_node *new = pecalloc(sizeof(*new), 1, 1); - new->next = new->data = new->head = NULL; - return new; -} - // Thanks to https://en.wikipedia.org/wiki/Insertion_sort :> sp_list_node *sp_list_sort(sp_list_node *pList, int (*cmp_func)(sp_list_node *, sp_list_node *)) { @@ -46,39 +40,26 @@ sp_list_node *sp_list_sort(sp_list_node *pList, return head; } -void sp_list_insert(sp_list_node *list, void *data) { - if (list->head == NULL) { - list->data = data; - list->next = NULL; - list->head = list; - } else { - sp_list_node *new = pecalloc(sizeof(*new), 1, 1); - - new->data = data; - new->next = NULL; - new->head = list; +sp_list_node *sp_list_insert(sp_list_node *list, void *data) { + sp_list_node *new = pecalloc(sizeof(*new), 1, 1); + sp_list_node *origin = list; + new->data = data; + new->next = NULL; + if (list == NULL) { + origin = new; + } else { while (list->next) { list = list->next; } list->next = new; } + return origin; } -void sp_list_prepend(sp_list_node *list, void *data) { - if (list->head == NULL) { - list->data = data; - list->next = NULL; - list->head = list; - } else { - sp_list_node *new = pecalloc(sizeof(*new), 1, 1); - - new->next = list->next; - list->next = new; - - new->head = list; - - new->data = list->data; - list->data = data; - } +sp_list_node *sp_list_prepend(sp_list_node *list, void *data) { + sp_list_node *new = pecalloc(sizeof(*new), 1, 1); + new->next = list; + new->data = data; + return new; } -- cgit v1.3