From 047c76825c9eea6b3475123e5627ee470c515934 Mon Sep 17 00:00:00 2001 From: Alessandro Iezzi Date: Mon, 13 Mar 2023 16:02:08 +0100 Subject: Change the logic to add_all collection We call clist_add inside the loop instead to play with pointers 'cause the data will be copied instead of referenced. So if the programmer clear the second list, the data will be stay there. --- src/list.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'src/list.c') diff --git a/src/list.c b/src/list.c index 542055d..3aa54bb 100644 --- a/src/list.c +++ b/src/list.c @@ -58,8 +58,13 @@ clist_add(list_t *list, void clist_add_all(list_t *dest, list_t *other) { - dest->last->next = other->first; - dest->size += other->size; + /* Concatenation of both lists must be iterated and not referenced becasue + * if you clear `other` you'll lost all elements */ + + iterator_t i = clist_iterator(other); + while (clist_iterator_has_next(i)) { + clist_add(dest, clist_iterator_next(&i)); + } } void clist_remove(list_t *list, -- cgit v1.2.3