Skip to content

Commit

Permalink
fix: change list length in data_structures/list/list.c (#1265)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aracnoid149 authored Jun 20, 2023
1 parent 8a3ff96 commit 8a1a497
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions data_structures/list/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down

0 comments on commit 8a1a497

Please sign in to comment.