-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstclear.c
79 lines (71 loc) · 2.2 KB
/
ft_lstclear.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akyoshid <akyoshid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/24 14:15:38 by akyoshid #+# #+# */
/* Updated: 2023/11/24 16:47:49 by akyoshid ### ########.fr */
/* */
/* ************************************************************************** */
// === DESCRIPTION ===
// Use the function `del` to free the content of the node,
// and then free the node itself.
// Execute this on the given node & every successor of that node.
// Finally, the pointer to the list must be set to NULL.
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *current_node;
t_list *next_node;
if (lst == NULL || *lst == NULL || del == NULL)
return ;
current_node = *lst;
while (current_node != NULL)
{
next_node = current_node->next;
(*del)(current_node->content);
free(current_node);
current_node = next_node;
}
*lst = NULL;
}
// static void ft_freecontent(void *content)
// {
// free(content);
// }
// static void ft_printlst(t_list *lst)
// {
// if (lst == NULL)
// {
// printf("lst is NULL.\n");
// return ;
// }
// while (lst != NULL)
// {
// if (lst->content == NULL)
// printf("content is freed\n");
// else
// printf("%s\n", (char *)lst->content);
// lst = lst->next;
// }
// }
// int main(void)
// {
// t_list *head;
// int i;
// i = 0;
// head = NULL;
// while (i < 10)
// {
// ft_lstadd_back(&head, ft_lstnew((void *)ft_itoa(i)));
// i++;
// }
// printf("Before ft_lstclear: \n");
// ft_printlst(head);
// printf("After ft_lstclear: \n");
// ft_lstclear(&head, ft_freecontent);
// ft_printlst(head);
// return (0);
// }