mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-29 16:53:12 +03:00
8a1a4972a5
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.
81 lines
1.3 KiB
C
81 lines
1.3 KiB
C
#include "list.h"
|
|
#include <assert.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#define L List_T
|
|
|
|
/* Initial list */
|
|
L List_init(void)
|
|
{
|
|
L list;
|
|
list = (L)malloc(sizeof(L));
|
|
list->next = NULL;
|
|
return list;
|
|
}
|
|
|
|
/* Push an element into top of the list */
|
|
L List_push(L list, void *val)
|
|
{
|
|
L new_elem = (L)malloc(sizeof(L));
|
|
new_elem->val = val;
|
|
new_elem->next = list;
|
|
return new_elem;
|
|
}
|
|
|
|
/* Length of list */
|
|
int List_length(L list)
|
|
{
|
|
int n;
|
|
for (n = 0; list; list = list->next) n++;
|
|
return n - 1;
|
|
}
|
|
|
|
/* Convert list to array */
|
|
void **List_toArray(L list)
|
|
{
|
|
int i, n = List_length(list) + 1;
|
|
void **array = (void **)malloc((n + 1) * sizeof(*array));
|
|
|
|
for (i = 0; i < n; i++)
|
|
{
|
|
array[i] = list->val;
|
|
list = list->next;
|
|
}
|
|
array[i] = NULL;
|
|
return array;
|
|
}
|
|
|
|
/* Create and return a list */
|
|
L List_list(L list, void *val, ...)
|
|
{
|
|
va_list ap;
|
|
L *p = &list;
|
|
|
|
va_start(ap, val);
|
|
for (; val; val = va_arg(ap, void *))
|
|
{
|
|
*p = malloc(sizeof(L));
|
|
(*p)->val = val;
|
|
p = &(*p)->next;
|
|
}
|
|
*p = NULL;
|
|
va_end(ap);
|
|
return list;
|
|
}
|
|
|
|
/* Append 2 lists together */
|
|
L List_append(L list, L tail)
|
|
{
|
|
L *p = &list;
|
|
while ((*p)->next)
|
|
{
|
|
p = &(*p)->next;
|
|
}
|
|
|
|
*p = tail;
|
|
return list;
|
|
}
|