From 3f2e2ae0f2c3250618333f9067e97a17663a5ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 21 Oct 2024 14:52:20 +0200 Subject: [PATCH] ft_lstdelone: initial implementation --- Makefile | 5 ++-- ft_lstdelone_bonus.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ libft.h | 3 ++- 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 ft_lstdelone_bonus.c diff --git a/Makefile b/Makefile index 3a8221d..50b1161 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/21 13:08:04 by kcolin ### ########.fr # +# Updated: 2024/10/21 14:49:31 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -51,7 +51,8 @@ BONUS_SOURCES = ft_lstnew_bonus.c \ ft_lstadd_front_bonus.c \ ft_lstsize_bonus.c \ ft_lstlast_bonus.c \ - ft_lstadd_back_bonus.c + ft_lstadd_back_bonus.c \ + ft_lstdelone_bonus.c BONUS_OBJECTS = $(BONUS_SOURCES:.c=.o) .PHONY: all diff --git a/ft_lstdelone_bonus.c b/ft_lstdelone_bonus.c new file mode 100644 index 0000000..5bdb362 --- /dev/null +++ b/ft_lstdelone_bonus.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/21 14:37:36 by kcolin #+# #+# */ +/* Updated: 2024/10/21 14:48:00 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +void ft_lstdelone(t_list *lst, void (*del)(void *)) +{ + (*del)(lst->content); + free(lst); +} + +/* +#include +#include + +int main(void) +{ + t_list *list; + t_list *new; + + list = ft_lstnew(ft_strdup("Hello There!")); + printf("current:\t%p\n", list); + printf("content:\t%s\n", (char *)list->content); + printf("next:\t\t%p\n", list->next); + printf("length:\t%d\n", ft_lstsize(list)); + new = ft_lstnew(ft_strdup("New Element!")); + ft_lstadd_front(&list, new); + printf("current:\t%p\n", list); + printf("content:\t%s\n", (char *)list->content); + printf("next:\t\t%p\n", list->next); + printf("length:\t%d\n", ft_lstsize(list)); + printf("last:\t\t%p\n", ft_lstlast(list)); + printf("=> deleting last..."); + ft_lstdelone(ft_lstlast(list), &free); + list->next = NULL; + printf("current:\t%p\n", list); + printf("content:\t%s\n", (char *)list->content); + printf("next:\t\t%p\n", list->next); + printf("length:\t%d\n", ft_lstsize(list)); + printf("last:\t\t%p\n", ft_lstlast(list)); + free(list->content); + free(list); + return (0); +} +*/ diff --git a/libft.h b/libft.h index 2023132..ade537c 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/21 12:11:28 by kcolin ### ########.fr */ +/* Updated: 2024/10/21 14:37:19 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,5 +74,6 @@ void ft_lstadd_front(t_list **lst, t_list *new); int ft_lstsize(t_list *lst); t_list *ft_lstlast(t_list *lst); void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void *)); #endif