From bc53060626ca78bce8fbb6dcbb7ecfc8e53db0ef Mon Sep 17 00:00:00 2001 From: hugogogo Date: Wed, 23 Mar 2022 21:52:04 +0100 Subject: [PATCH] add lstremove_next and lstadd_back now return --- Makefile | 3 ++- includes/libft.h | 5 +++-- srcs/ft_lstadd_back.c | 12 ++++++++---- srcs/ft_lstnew.c | 10 ++++------ srcs/ft_lstremove_next.c | 30 ++++++++++++++++++++++++++++++ 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 srcs/ft_lstremove_next.c diff --git a/Makefile b/Makefile index ce72c00..c3937a8 100644 --- a/Makefile +++ b/Makefile @@ -120,7 +120,8 @@ SRCS = ft_memset.c \ ft_sqrt.c \ ft_free_tab.c \ \ - ft_arrintchr.c + ft_arrintchr.c \ + ft_lstremove_next.c ODIR = ./builds diff --git a/includes/libft.h b/includes/libft.h index da6babe..56b1d85 100644 --- a/includes/libft.h +++ b/includes/libft.h @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/25 14:45:53 by hulamy #+# #+# */ -/* Updated: 2022/03/23 14:22:33 by simplonco ### ########.fr */ +/* Updated: 2022/03/23 21:51:14 by simplonco ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,9 +65,9 @@ typedef struct s_list t_list *ft_lstnew(void *content); void ft_lstadd_front(t_list **alst, t_list *n); +void *ft_lstadd_back(t_list **alst, t_list *n); int ft_lstsize(t_list *lst); t_list *ft_lstlast(t_list *lst); -void ft_lstadd_back(t_list **alst, t_list *n); void ft_lstdelone(t_list *lst, void (*del)(void *)); void ft_lstclear(t_list **lst, void (*del)(void *)); void ft_lstiter(t_list *lst, void (*f)(void *)); @@ -114,5 +114,6 @@ int ft_sqrt(int i); void ft_free_tab(char **tab); int ft_arrintchr(int * intarr, int comp, size_t size); +void ft_lstremove_next(t_list *lst, void (*del)(void *)); #endif diff --git a/srcs/ft_lstadd_back.c b/srcs/ft_lstadd_back.c index 315119b..dceb7e0 100644 --- a/srcs/ft_lstadd_back.c +++ b/srcs/ft_lstadd_back.c @@ -6,13 +6,14 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/25 14:11:53 by hulamy #+# #+# */ -/* Updated: 2019/11/25 14:36:12 by hulamy ### ########.fr */ +/* Updated: 2022/03/23 21:50:42 by simplonco ### ########.fr */ /* */ /* ************************************************************************** */ /* -** add an element to the end of a list -** or first if list has no element so far +** add an element to the end of a list, or first if list has no element yet +** return NULL if element is NULL (eg, it returned from ft_lstnew and failed) +** or else address to the element added */ /* @@ -71,10 +72,12 @@ #include "libft.h" -void ft_lstadd_back(t_list **alst, t_list *new) +void *ft_lstadd_back(t_list **alst, t_list *new) { t_list *tmp; + if (!new) + return (NULL); if (alst) { tmp = *alst; @@ -87,4 +90,5 @@ void ft_lstadd_back(t_list **alst, t_list *new) tmp->next = new; } } + return (tmp); } diff --git a/srcs/ft_lstnew.c b/srcs/ft_lstnew.c index 401f6c9..0c64f4e 100644 --- a/srcs/ft_lstnew.c +++ b/srcs/ft_lstnew.c @@ -6,7 +6,7 @@ /* By: hulamy +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/11/25 14:16:20 by hulamy #+# #+# */ -/* Updated: 2019/11/25 14:29:46 by hulamy ### ########.fr */ +/* Updated: 2022/03/23 20:24:40 by simplonco ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,12 +48,10 @@ t_list *ft_lstnew(void *content) { t_list *lst; - if (!(lst = (t_list *)malloc(sizeof(*lst)))) + lst = (t_list *)malloc(sizeof(*lst)); + if (!lst) return (NULL); - if (!content) - lst->content = NULL; - else - lst->content = content; + lst->content = content; lst->next = NULL; return (lst); } diff --git a/srcs/ft_lstremove_next.c b/srcs/ft_lstremove_next.c new file mode 100644 index 0000000..926f5d5 --- /dev/null +++ b/srcs/ft_lstremove_next.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstremove_next.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: simplonco +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/03/23 21:42:05 by simplonco #+# #+# */ +/* Updated: 2022/03/23 21:42:55 by simplonco ### ########.fr */ +/* */ +/* ************************************************************************** */ + +/* + * remove the next element of the list, delete its content, and free it + * then rejoin the list without this element + */ + +#include "libft.h" + +void ft_lstremove_next(t_list *lst, void (*del)(void *)) +{ + t_list *next_tmp; + + if (!lst || !lst->next || !del) + return ; + next_tmp = lst->next->next; + del(lst->next->content); + free(lst->next); + lst->next = next_tmp; +}