From 75ff581de3ee58726f98b442560cbebaa60216e7 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sun, 25 Dec 2016 15:55:48 -0800 Subject: [PATCH] Add clear function that clears the linked list. --- include/ll.h | 7 +++++++ src/ll.c | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/include/ll.h b/include/ll.h index fe27590..641ab8b 100644 --- a/include/ll.h +++ b/include/ll.h @@ -109,5 +109,12 @@ void* ll_pophead(ll* ll); */ void* ll_poptail(ll* ll); +/** + * Removes all elements from the list, freeing the underlying memory + * but keeping the data intact, + * @param ll the linked list to clear. + */ +void ll_clear(ll* ll); + #endif diff --git a/src/ll.c b/src/ll.c index bbb0da1..599ec30 100644 --- a/src/ll.c +++ b/src/ll.c @@ -113,3 +113,14 @@ void* ll_poptail(ll* ll) { } return to_return; } + +void ll_clear(ll* ll){ + ll_node* head = ll->head; + while(ll->head){ + ll_node* to_free = head; + head = head->next; + free(to_free); + } + ll->head = NULL; + ll->tail = NULL; +}