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

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);
}
*/