summaryrefslogtreecommitdiff
path: root/graphs/piscine/generic_void_list/list.c
blob: 20ecfa82b52f47540274ae8e00b6dbbc022a02b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "list.h"

#include <stdlib.h>
#include <string.h>

struct list *list_prepend(struct list *list, const void *value,
                          size_t data_size)
{
    struct list *new = malloc(sizeof(struct list));
    new->next = list;
    new->data = malloc(sizeof(void *));
    memcpy(new->data, value, data_size);
    return new;
}

size_t list_length(struct list *list)
{
    size_t res = 0;
    while (list)
    {
        res++;
        list = list->next;
    }
    return res;
}

void list_destroy(struct list *list)
{
    while (list)
    {
        struct list *tmp = list->next;
        free(list->data);
        free(list);
        list = tmp;
    }
}