diff options
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) { |