From cf6bb620300d5f003f28a23733e43b057370b906 Mon Sep 17 00:00:00 2001 From: gbrochar Date: Fri, 25 Oct 2024 14:45:15 +0200 Subject: [PATCH] fix: lstdel && feat: del red black tree --- ft_lstdel.c | 12 +++++------- ft_rb_tree.c | 11 +++++++++++ libft.h | 3 ++- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/ft_lstdel.c b/ft_lstdel.c index 9c02c08..8f03b2d 100644 --- a/ft_lstdel.c +++ b/ft_lstdel.c @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2015/11/30 21:34:50 by gbrochar #+# #+# */ -/* Updated: 2015/12/05 15:10:37 by gbrochar ### ########.fr */ +/* Updated: 2024/10/25 14:32:43 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,12 +14,10 @@ void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) { - t_list *tmp; - - while (*alst) + if (*alst) { - tmp = *alst; - ft_lstdelone(alst, del); - *alst = tmp->next; + ft_lstdel(&(*alst)->next, del); + del((*alst)->content, (*alst)->content_size); + ft_memdel((void **)alst); } } diff --git a/ft_rb_tree.c b/ft_rb_tree.c index 85bfdc1..9086581 100644 --- a/ft_rb_tree.c +++ b/ft_rb_tree.c @@ -1,5 +1,16 @@ #include "libft.h" +void ft_delrbt(t_node *node, void (*f)(void *data)) { + if (!(node)) { + return; + } + ft_delrbt(node->left, f); + ft_delrbt(node->right, f); + f((void *)node); + free(node->data); + free(node); +} + void ft_putrbt(t_node *node, void (*f)(void *data)) { if (!(node)) { return; diff --git a/libft.h b/libft.h index 8da7af4..db17268 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: gbrochar +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2015/11/24 12:52:28 by gbrochar #+# #+# */ -/* Updated: 2024/10/20 18:36:39 by gbrochar ### ########.fr */ +/* Updated: 2024/10/25 13:56:13 by gbrochar ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,7 @@ typedef t_node t_leaf; void ft_rbt_insert(t_root **root, t_node *z, int (*f)(void *a, void *b)); t_node *ft_rbt_new(void *data, size_t size); void ft_putrbt(t_node *node, void (*f)(void *data)); +void ft_delrbt(t_node *node, void (*f)(void *data)); void ft_lstadd(t_list **alst, t_list *new); void ft_lstadd_back(t_list **alst, t_list *new); void ft_lstinsert(t_list **lst, t_list *new, int (*f)(t_list *a, t_list *b));