Created a "free_list" method to free up allocated memory.

pull/4/head
Jacob Windle 2020-01-09 09:46:41 -05:00
parent 7d57550c06
commit 753b086e4f
3 changed files with 16 additions and 1 deletions

View File

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

View File

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

2
main.c
View File

@ -192,6 +192,8 @@ int main(int argc, char **argv)
// daemonize();
}
}
free_list(&hosts);
}
/**