From 8a1a4972a58ea279162626cc66dc6b796a0423e5 Mon Sep 17 00:00:00 2001 From: Ethan Fredsti Date: Tue, 20 Jun 2023 14:07:09 -0700 Subject: [PATCH] fix: change list length in `data_structures/list/list.c` (#1265) I changed the return value of n in List_length to reflect the number of items inside the list, so a newly initialized list will return a length of 0. To prevent items in List_toArray from being cut off, I addeone back to n at the beginning of the List_toArray function. --- data_structures/list/list.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/list/list.c b/data_structures/list/list.c index 9731acf0..762ffdfa 100644 --- a/data_structures/list/list.c +++ b/data_structures/list/list.c @@ -30,13 +30,13 @@ int List_length(L list) { int n; for (n = 0; list; list = list->next) n++; - return n; + return n - 1; } /* Convert list to array */ void **List_toArray(L list) { - int i, n = List_length(list); + int i, n = List_length(list) + 1; void **array = (void **)malloc((n + 1) * sizeof(*array)); for (i = 0; i < n; i++)