diff options
Diffstat (limited to 'rushs/tinyprintf/generic_void_list')
| -rw-r--r-- | rushs/tinyprintf/generic_void_list/list.c | 36 | ||||
| -rw-r--r-- | rushs/tinyprintf/generic_void_list/list.h | 31 |
2 files changed, 67 insertions, 0 deletions
diff --git a/rushs/tinyprintf/generic_void_list/list.c b/rushs/tinyprintf/generic_void_list/list.c new file mode 100644 index 0000000..20ecfa8 --- /dev/null +++ b/rushs/tinyprintf/generic_void_list/list.c @@ -0,0 +1,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; + } +} diff --git a/rushs/tinyprintf/generic_void_list/list.h b/rushs/tinyprintf/generic_void_list/list.h new file mode 100644 index 0000000..a1bc035 --- /dev/null +++ b/rushs/tinyprintf/generic_void_list/list.h @@ -0,0 +1,31 @@ +#ifndef LIST_H +#define LIST_H + +#include <stddef.h> + +struct list +{ + void *data; + struct list *next; +}; + +/* +** Insert a node containing `value` at the beginning of the list. +** Return `NULL` if an error occurred. +*/ +struct list *list_prepend(struct list *list, const void *value, + size_t data_size); + +/* +** Return the length of the list. +** Return `0` if the list is empty. +*/ +size_t list_length(struct list *list); + +/* +** Release the memory used by the list. +** Does nothing if `list` is `NULL`. +*/ +void list_destroy(struct list *list); + +#endif /* !LIST_H */ |
