Skip to content

Commit 8a1a497

Browse files
authored
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.
1 parent 8a3ff96 commit 8a1a497

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

data_structures/list/list.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ int List_length(L list)
3030
{
3131
int n;
3232
for (n = 0; list; list = list->next) n++;
33-
return n;
33+
return n - 1;
3434
}
3535

3636
/* Convert list to array */
3737
void **List_toArray(L list)
3838
{
39-
int i, n = List_length(list);
39+
int i, n = List_length(list) + 1;
4040
void **array = (void **)malloc((n + 1) * sizeof(*array));
4141

4242
for (i = 0; i < n; i++)

0 commit comments

Comments
 (0)