mirror of https://github.com/TheAlgorithms/C
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:
parent
8a3ff966e7
commit
8a1a4972a5
|
@ -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++)
|
||||||
|
|
Loading…
Reference in New Issue