Added list dumper

This commit is contained in:
MarcUs7i 2025-03-26 19:10:24 +01:00
parent 4975231933
commit 59da024f42

View file

@ -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;
}