From d55488c605c425815c641f22aa46c398da20a950 Mon Sep 17 00:00:00 2001 From: mrb0nk500 Date: Thu, 28 Jul 2022 13:51:42 -0300 Subject: linked_list: Add `linked_list_to_array()` This function takes the given linked list, and creates a type generic pointer array from it. --- linked_list.c | 13 +++++++++++++ linked_list.h | 1 + 2 files changed, 14 insertions(+) diff --git a/linked_list.c b/linked_list.c index 3c694be..0eeb19b 100644 --- a/linked_list.c +++ b/linked_list.c @@ -62,6 +62,19 @@ void cleanup_linked_list(linked_list *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); diff --git a/linked_list.h b/linked_list.h index d18b859..a89578d 100644 --- a/linked_list.h +++ b/linked_list.h @@ -14,6 +14,7 @@ extern linked_list *get_tail(linked_list *node); extern linked_list *add_node(linked_list **tail, void *data); extern void remove_node(linked_list *node); extern void cleanup_linked_list(linked_list *node); +extern void **linked_list_to_array(linked_list *node); extern int linked_list_size(linked_list *tail); #endif -- cgit v1.2.3-13-gbd6f