#include #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; } else { linked_list *node = calloc(1, sizeof(linked_list)); if (node == NULL) { return NULL; } else { node->next = NULL; node->data = data; if (*tail == NULL) { node->prev = NULL; *tail = node; } else { (*tail)->next = node; node->prev = *tail; } return node; } } } void remove_node(linked_list *node) { if (node != NULL) { if (node->prev != NULL) { node->prev->next = node->next; } if (node->next != NULL) { node->next->prev = node->prev; } free(node); } } 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); } } void **linked_list_to_array(linked_list *node) { void **arr = calloc(linked_list_size(get_tail(node))+1, sizeof(void *)); int i = 0; for (linked_list *n = get_head(node); n != NULL; n = n->next, ++i) { arr[i] = n->data; } arr[i] = NULL; return arr; } int linked_list_size(linked_list *tail) { int i = 0; for (linked_list *node = tail; node != NULL; node = node->prev, ++i); return i; }