From 9d35ddb703d7ba22bdd6cdbaaf3b5c992ec79025 Mon Sep 17 00:00:00 2001 From: sanine Date: Fri, 24 Oct 2025 15:14:07 -0500 Subject: add list copy --- lichen.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) (limited to 'lichen.c') diff --git a/lichen.c b/lichen.c index b0b0684..d7f0f82 100644 --- a/lichen.c +++ b/lichen.c @@ -77,10 +77,10 @@ void li_free_ll(struct li_ll_t *list, void (*free_data)(void*)) { } -void li_ll_append(struct li_ll_t *list, void *data) { +int li_ll_append(struct li_ll_t *list, void *data) { li_llnode_t *n = li_alloc_llnode(); if (n == NULL) { - return; + return 1; } n->data = data; if (list->end != NULL) { @@ -91,13 +91,14 @@ void li_ll_append(struct li_ll_t *list, void *data) { list->end = n; } list->count += 1; + return 0; } -void li_ll_prepend(struct li_ll_t *list, void *data) { +int li_ll_prepend(struct li_ll_t *list, void *data) { li_llnode_t *n = li_alloc_llnode(); if (n == NULL) { - return; + return 1; } n->data = data; if (list->head != NULL) { @@ -108,10 +109,27 @@ void li_ll_prepend(struct li_ll_t *list, void *data) { list->end = n; } list->count += 1; + return 0; } -struct li_ll_t * li_copy_list(struct li_ll_t *list) { - list = list; - return NULL; +int li_copy_list(struct li_ll_t **dst, struct li_ll_t *src, void *(*copy_data)(void*)) { + *dst = li_alloc_ll(); + if (*dst == NULL) { + return 1; + } + + (*dst)->tag = src->tag; + + for (li_llnode_t *n = src->head; n != NULL; n = n->next) { + void *dup = copy_data(n->data); + if (dup == NULL) { + return 1; + } + if (li_ll_append(*dst, dup) != 0) { + return 1; + } + } + + return 0; } -- cgit v1.2.1