diff options
author | Vincent Douillet <vincent@vdouillet.fr> | 2021-12-07 18:13:24 +0100 |
---|---|---|
committer | Vincent Douillet <vincent@vdouillet.fr> | 2021-12-07 18:13:24 +0100 |
commit | daad1e7e3abf5a27a41d8b5e52a4536068030327 (patch) | |
tree | ab18a31fd590e40c8483ff992761f2d97a4f88d9 /list.c | |
parent | 7d65acce058a99a8b33d6d2870ac8bbb939827d8 (diff) |
day 6
Diffstat (limited to 'list.c')
-rw-r--r-- | list.c | 46 |
1 files changed, 46 insertions, 0 deletions
@@ -0,0 +1,46 @@ +#include <err.h> +#include "list.h" + +#define LIST_SIZE 50 + +struct long_list* long_list_init(size_t size) { + struct long_list* list = malloc(sizeof(struct long_list)); + list->list = malloc(sizeof(long) * size); + list->alloc_size = size; + list->length = 0; + return list; +} + +void long_list_free(struct long_list* list) { + if(list == NULL) + return; + + if(list->list != NULL) + free(list->list); + + free(list); +} + +void long_list_add(struct long_list* list, long value) { + // increase space if needed + if(list->length >= list->alloc_size) { + list->list = realloc(list->list, sizeof(long) * (list->alloc_size + LIST_SIZE)); + list->alloc_size += LIST_SIZE; + } + list->list[list->length] = value; + list->length = list->length + 1; +} + +long long_list_get(struct long_list* list, size_t index) { + if(index >= list->length) + err(1, "index %ld out of bounds (%ld)", index, list->length); + + return list->list[index]; +} + +void long_list_set(struct long_list* list, size_t index, long value) { + if(index >= list->length) + err(1, "index %ld out of bounds (%ld)", index, list->length); + + list->list[index] = value; +} |