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 /graphs/piscine/dlist/dlist-1.c | |
add: graphs et rushs
Diffstat (limited to 'graphs/piscine/dlist/dlist-1.c')
| -rw-r--r-- | graphs/piscine/dlist/dlist-1.c | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/graphs/piscine/dlist/dlist-1.c b/graphs/piscine/dlist/dlist-1.c new file mode 100644 index 0000000..443ebca --- /dev/null +++ b/graphs/piscine/dlist/dlist-1.c @@ -0,0 +1,78 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "dlist.h" + +struct dlist *dlist_init(void) +{ + struct dlist *res = malloc(sizeof(struct dlist)); + if (res == NULL) + return NULL; + + res->size = 0; + res->head = NULL; + res->tail = NULL; + return res; +} + +int dlist_push_front(struct dlist *list, int element) +{ + if (element < 0) + return 0; + struct dlist_item *new = malloc(sizeof(struct dlist_item)); + if (new == NULL) + return 0; + + new->data = element; + new->next = list->head; + new->prev = NULL; + + if (list->size == 0) + list->tail = new; + else + list->head->prev = new; + + list->head = new; + list->size++; + + return 1; +} + +void dlist_print(const struct dlist *list) +{ + if (list->size == 0) + return; + + for (struct dlist_item *i = list->head; i != list->tail; i = i->next) + { + printf("%d\n", i->data); + } + printf("%d\n", list->tail->data); +} + +int dlist_push_back(struct dlist *list, int element) +{ + if (element < 0) + return 0; + struct dlist_item *new = malloc(sizeof(struct dlist_item)); + if (new == NULL) + return 0; + + new->data = element; + new->prev = list->tail; + new->next = NULL; + + if (list->size == 0) + list->head = new; + else + list->tail->next = new; + list->tail = new; + list->size++; + + return 1; +} + +size_t dlist_size(const struct dlist *list) +{ + return list->size; +} |
