Created a "free_list" method to free up allocated memory.
parent
7d57550c06
commit
753b086e4f
14
linkedlist.c
14
linkedlist.c
|
@ -48,8 +48,20 @@ void linkedlist_print(LinkedList *head) {
|
|||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
void free_list(LinkedList *head) {
|
||||
|
||||
void free_list(LinkedList **head) {
|
||||
LinkedList *ptr = *head;
|
||||
LinkedList *prev = NULL;
|
||||
|
||||
if (ptr == NULL)
|
||||
return;
|
||||
|
||||
while (ptr != NULL) {
|
||||
prev = ptr;
|
||||
ptr = ptr->next;
|
||||
free(prev->data);
|
||||
free(prev);
|
||||
}
|
||||
}
|
||||
void free_node(LinkedList *node) {
|
||||
free(node->data);
|
||||
|
|
|
@ -18,4 +18,5 @@ typedef struct linkedlist {
|
|||
LinkedList *linkedlist_new();
|
||||
void linkedlist_add(LinkedList **head, char *data);
|
||||
void linkedlist_print(LinkedList *head);
|
||||
void free_list(LinkedList **head);
|
||||
#endif //HOSTBLOCKER_LINKEDLIST_H
|
||||
|
|
Loading…
Reference in New Issue