From 59da024f4256d5f3647adfa6530c1c0fb10e2078 Mon Sep 17 00:00:00 2001 From: MarcUs7i Date: Wed, 26 Mar 2025 19:10:24 +0100 Subject: [PATCH] Added list dumper --- doubly_linked_list_with_iterator.c | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/doubly_linked_list_with_iterator.c b/doubly_linked_list_with_iterator.c index 6ab7756..f0942fd 100644 --- a/doubly_linked_list_with_iterator.c +++ b/doubly_linked_list_with_iterator.c @@ -39,6 +39,7 @@ #include "doubly_linked_list_with_iterator.h" #include "limits.h" #include "allocator.h" +#include "stdio.h" /** The type of list nodes */ typedef struct IntListNodeData* IntListNode; @@ -77,10 +78,39 @@ static IntListNode list_obtain_node(int data) { static void list_release_node(IntListNode node) { free_mem(node); + node = 0; } /* optional: implement a function for printing the content of the list - may be useful for debugging */ -void list_dump(char* prefix, IntList list) { +void list_dump(IntList list) { + char list_dumper_title[13] = "List dumper:"; + if(!list_is_valid(list)) { + printf("%s Got invalid list\n", list_dumper_title); + return; + } + + if (list_is_empty(list)) { + printf("%s Got an empty list\n", list_dumper_title); + return; + } + + printf("%s Got a list with size %d\n", list_dumper_title ,list->size); + + IntListIterator iterator = list_it_obtain(list); + if (iterator == 0) { + printf("%s An error occured while trying to obtain the list iterator\n", list_dumper_title); + return; + } + + + for (int i = 0; i < list->size; i++) { + printf("%s Node %d with value %d\n", list_dumper_title, i, list_it_get(iterator)); + if(!list_it_next(iterator)) { // early break, due to being already at the end of the list + break; + } + } + + list_it_release(iterator); } /* ===================================================================== */ @@ -165,7 +195,7 @@ bool list_is_empty(IntList list) { * @return The number of values the list contains. */ int list_get_size(IntList list) { - if(!list_is_empty(list)) { + if(!list_is_valid(list)) { return 0; }