ft_lstclear: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-21 15:18:48 +02:00
parent 3f2e2ae0f2
commit f9e81f344c
3 changed files with 63 additions and 2 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
# Updated: 2024/10/21 14:49:31 by kcolin ### ########.fr #
# Updated: 2024/10/21 15:03:15 by kcolin ### ########.fr #
# #
# **************************************************************************** #
@ -52,7 +52,8 @@ BONUS_SOURCES = ft_lstnew_bonus.c \
ft_lstsize_bonus.c \
ft_lstlast_bonus.c \
ft_lstadd_back_bonus.c \
ft_lstdelone_bonus.c
ft_lstdelone_bonus.c \
ft_lstclear_bonus.c
BONUS_OBJECTS = $(BONUS_SOURCES:.c=.o)
.PHONY: all

59
ft_lstclear_bonus.c Normal file
View file

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/21 14:53:58 by kcolin #+# #+# */
/* Updated: 2024/10/21 15:17:38 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
if (*lst == NULL)
return ;
if ((*lst)->next != NULL)
{
ft_lstclear(&((*lst)->next), del);
ft_lstdelone(*lst, del);
*lst = NULL;
}
else
{
ft_lstdelone(*lst, del);
*lst = NULL;
}
}
/*
#include <stdio.h>
#include <stdlib.h>
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...\n");
ft_lstclear(&list, &free);
ft_lstclear(&list, &free);
printf("current:\t%p\n", list);
return (0);
}
*/

View file

@ -75,5 +75,6 @@ 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 *));
void ft_lstclear(t_list **lst, void (*del)(void *));
#endif