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.
This commit is contained in:
Ethan Fredsti 2023-06-20 14:07:09 -07:00 committed by GitHub
parent 8a3ff966e7
commit 8a1a4972a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -30,13 +30,13 @@ int List_length(L list)
{ {
int n; int n;
for (n = 0; list; list = list->next) n++; for (n = 0; list; list = list->next) n++;
return n; return n - 1;
} }
/* Convert list to array */ /* Convert list to array */
void **List_toArray(L list) 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)); void **array = (void **)malloc((n + 1) * sizeof(*array));
for (i = 0; i < n; i++) for (i = 0; i < n; i++)