diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /rushs/evalexpr/generic_void_list | |
add: graphs et rushs
Diffstat (limited to 'rushs/evalexpr/generic_void_list')
| -rw-r--r-- | rushs/evalexpr/generic_void_list/list.c | 36 | ||||
| -rw-r--r-- | rushs/evalexpr/generic_void_list/list.h | 31 |
2 files changed, 67 insertions, 0 deletions
diff --git a/rushs/evalexpr/generic_void_list/list.c b/rushs/evalexpr/generic_void_list/list.c new file mode 100644 index 0000000..20ecfa8 --- /dev/null +++ b/rushs/evalexpr/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/evalexpr/generic_void_list/list.h b/rushs/evalexpr/generic_void_list/list.h new file mode 100644 index 0000000..a1bc035 --- /dev/null +++ b/rushs/evalexpr/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 */ |
