diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-27 08:40:12 -0300 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2022-07-27 08:58:27 -0300 |
commit | 1d6d32d044e67fc984e5c1b8361460844fec52db (patch) | |
tree | 47c858fcd3cd1597844e7cf989481ed5914337aa /linked_list.c | |
parent | 9edc129c0d675b80aee9d2f382bd80ed9a43ddba (diff) |
linked_list: Add `cleanup_linked_list()`
This function cleans up a linked list by removing every node in the
list.
Diffstat (limited to 'linked_list.c')
-rw-r--r-- | linked_list.c | 32 |
1 files changed, 21 insertions, 11 deletions
diff --git a/linked_list.c b/linked_list.c index 792da4d..3c694be 100644 --- a/linked_list.c +++ b/linked_list.c @@ -1,6 +1,18 @@ #include <stdlib.h> #include "linked_list.h" +linked_list *get_head(linked_list *node) { + linked_list *head; + for (head = node; head != NULL && head->prev != NULL; head = head->prev); + return head; +} + +linked_list *get_tail(linked_list *node) { + linked_list *tail; + for (tail = node; tail != NULL && tail->next != NULL; tail = tail->next); + return tail; +} + linked_list *add_node(linked_list **tail, void *data) { if (tail == NULL) { return NULL; @@ -25,7 +37,6 @@ linked_list *add_node(linked_list **tail, void *data) { } } - void remove_node(linked_list *node) { if (node != NULL) { if (node->prev != NULL) { @@ -40,16 +51,15 @@ void remove_node(linked_list *node) { } } -linked_list *get_head(linked_list *node) { - linked_list *head; - for (head = node; head != NULL && head->prev != NULL; head = head->prev); - return head; -} - -linked_list *get_tail(linked_list *node) { - linked_list *tail; - for (tail = node; tail != NULL && tail->next != NULL; tail = tail->next); - return tail; +void cleanup_linked_list(linked_list *node) { + if (node != NULL) { + for (node = get_tail(node); node != NULL; node = node->prev) { + if (node->next != NULL) { + remove_node(node->next); + } + } + remove_node(node); + } } int linked_list_size(linked_list *tail) { |