From 949cbd73557774b217c84a57159f141325e5333a Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 28 Jun 2023 10:46:44 +0200 Subject: [PATCH] add: lst --- src/lst/lst.h | 4 ++-- src/lst/lst_addback.c | 18 ++++++++++++++++++ src/lst/lst_find.c | 14 ++++++++++++++ src/lst/lst_iter.c | 12 ++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 src/lst/lst_addback.c create mode 100644 src/lst/lst_find.c create mode 100644 src/lst/lst_iter.c diff --git a/src/lst/lst.h b/src/lst/lst.h index f01108c..d14f38a 100644 --- a/src/lst/lst.h +++ b/src/lst/lst.h @@ -8,7 +8,7 @@ typedef struct s_lst } lst; size_t lst_len(const lst* root); -void lst_iter(const lst* root, void (*f)(void *)); -lst* lst_find(lst* root, void (*cmp)(const lst* lst1, const lst* lst2)); +void lst_iter(lst** root, void (*f)(void *)); +lst* lst_find(lst** root, const lst* to_find, int (*cmp)(const lst* lst1, const lst* lst2)); void lst_addback(lst** root, lst* nouveau); void lst_clear(lst** root, void (*del)(void *)); diff --git a/src/lst/lst_addback.c b/src/lst/lst_addback.c new file mode 100644 index 0000000..5f80d92 --- /dev/null +++ b/src/lst/lst_addback.c @@ -0,0 +1,18 @@ +#include "lst.h" + +void lst_addback(lst **root, lst *nouveau) +{ + lst* current = *root; + + if (root == NULL || nouveau == NULL) + return; + nouveau->next = NULL; + if (current == NULL) + { + current = nouveau; + return; + } + while (current->next != NULL) + current = current->next; + current->next = nouveau; +} diff --git a/src/lst/lst_find.c b/src/lst/lst_find.c new file mode 100644 index 0000000..bc279b1 --- /dev/null +++ b/src/lst/lst_find.c @@ -0,0 +1,14 @@ +#include "./lst.h" + +lst* lst_find(lst** root, const lst* to_find, int (*cmp)(const lst* lst1, const lst* lst2)) +{ + lst* current = *root; + + while (current != NULL) + { + if ((*cmp)(to_find, current) == 0) + return current; + current = current->next; + } + return (NULL); +} diff --git a/src/lst/lst_iter.c b/src/lst/lst_iter.c new file mode 100644 index 0000000..fb8210c --- /dev/null +++ b/src/lst/lst_iter.c @@ -0,0 +1,12 @@ +#include "./lst.h" + +void lst_iter(lst** root, void (*f)(void *)) +{ + lst *current = *root; + + while (current != NULL) + { + (*f)(current); + current = current->next; + } +}