Add clear function that clears the linked list.

This commit is contained in:
Danila Fedorin 2016-12-25 15:55:48 -08:00
parent 4c7f0ee0e5
commit 75ff581de3
2 changed files with 18 additions and 0 deletions

View File

@ -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

View File

@ -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;
}