From 08639517874af66173f621e6c15b1481bbfb8074 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zo=C3=ABy=20Noort?= Date: Sat, 31 May 2025 19:48:39 +0200 Subject: [PATCH] feat: added more lst functionality --- srcs/lst/ft_lstclear.c | 13 +++++++++++++ srcs/lst/ft_lstempty.c | 6 ++++++ srcs/lst/ft_lstfind.c | 12 ++++++++++++ srcs/lst/ft_lstget.c | 12 ++++++++++++ srcs/lst/ft_lstinsert.c | 30 ++++++++++++++++++++++++++++++ srcs/lst/ft_lstiter.c | 10 ++++++++++ srcs/lst/ft_lstreverse.c | 17 +++++++++++++++++ 7 files changed, 100 insertions(+) create mode 100644 srcs/lst/ft_lstclear.c create mode 100644 srcs/lst/ft_lstempty.c create mode 100644 srcs/lst/ft_lstfind.c create mode 100644 srcs/lst/ft_lstget.c create mode 100644 srcs/lst/ft_lstinsert.c create mode 100644 srcs/lst/ft_lstiter.c create mode 100644 srcs/lst/ft_lstreverse.c diff --git a/srcs/lst/ft_lstclear.c b/srcs/lst/ft_lstclear.c new file mode 100644 index 0000000..5d736dd --- /dev/null +++ b/srcs/lst/ft_lstclear.c @@ -0,0 +1,13 @@ +#include "libft.h" +#include + +void ft_lstclear(t_list **lst) +{ + t_list *tmp; + while (lst && *lst) + { + tmp = (*lst)->next; + free(*lst); + *lst = tmp; + } +} \ No newline at end of file diff --git a/srcs/lst/ft_lstempty.c b/srcs/lst/ft_lstempty.c new file mode 100644 index 0000000..84d5e21 --- /dev/null +++ b/srcs/lst/ft_lstempty.c @@ -0,0 +1,6 @@ +#include "libft.h" + +int ft_lstempty(t_list *lst) +{ + return (lst == NULL); +} \ No newline at end of file diff --git a/srcs/lst/ft_lstfind.c b/srcs/lst/ft_lstfind.c new file mode 100644 index 0000000..aff4f59 --- /dev/null +++ b/srcs/lst/ft_lstfind.c @@ -0,0 +1,12 @@ +#include "libft.h" + +t_list *ft_lstfind(t_list *lst, void *data, int (*cmp)(void *, void *)) +{ + while (lst) + { + if (cmp(lst->data, data) == 0) + return (lst); + lst = lst->next; + } + return (NULL); +} \ No newline at end of file diff --git a/srcs/lst/ft_lstget.c b/srcs/lst/ft_lstget.c new file mode 100644 index 0000000..f2f5dc1 --- /dev/null +++ b/srcs/lst/ft_lstget.c @@ -0,0 +1,12 @@ +#include "libft.h" + +t_list *ft_lstget(t_list *lst, int index) +{ + int i = 0; + while (lst && i < index) + { + lst = lst->next; + i++; + } + return ((i == index) ? lst : NULL); +} diff --git a/srcs/lst/ft_lstinsert.c b/srcs/lst/ft_lstinsert.c new file mode 100644 index 0000000..dae06da --- /dev/null +++ b/srcs/lst/ft_lstinsert.c @@ -0,0 +1,30 @@ +#include "libft.h" + +int ft_lstinsert(t_list **lst, t_list *node, int index) +{ + t_list *curr = *lst; + t_list *prev = NULL; + int i = 0; + + if (index < 0 || !node) + return (-1); + if (index == 0) + { + node->next = *lst; + *lst = node; + return (0); + } + while (curr && i < index) + { + prev = curr; + curr = curr->next; + i++; + } + if (i == index) + { + prev->next = node; + node->next = curr; + return 0; + } + return (-1); +} diff --git a/srcs/lst/ft_lstiter.c b/srcs/lst/ft_lstiter.c new file mode 100644 index 0000000..4556347 --- /dev/null +++ b/srcs/lst/ft_lstiter.c @@ -0,0 +1,10 @@ +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + while (lst) + { + f(lst->data); + lst = lst->next; + } +} diff --git a/srcs/lst/ft_lstreverse.c b/srcs/lst/ft_lstreverse.c new file mode 100644 index 0000000..af8b57d --- /dev/null +++ b/srcs/lst/ft_lstreverse.c @@ -0,0 +1,17 @@ +#include "libft.h" + +void ft_lstreverse(t_list **lst) +{ + t_list *prev = NULL; + t_list *curr = *lst; + t_list *next; + + while (curr) + { + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + *lst = prev; +}