-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstclear.c
86 lines (67 loc) · 1.95 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
80
81
82
83
84
85
86
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: musenov <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/28 13:58:36 by musenov #+# #+# */
/* Updated: 2022/12/08 18:43:53 by musenov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *buffer;
if (!lst || !del)
return ;
while (*lst)
{
buffer = (**lst).next;
del((**lst).content);
free(*lst);
*lst = buffer;
}
*lst = NULL;
}
/*
ft_lstclear:
PARAMETERS
lst: The address of a pointer to a node.
del: The address of the function used to delete the content of the node.
RETURN VALUE
None
DESCRIPTION
Deletes and frees the given node and every successor of that node,
using the function ’del’ and free(3). Finally, the pointer to the
list must be set to NULL.
QUESTIONS
-/-
ANSWER
-/-
COMPARE
-/-
ALTERNATIVE SOLUTION
-/-
EXPLANATION
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *buffer;
if (!lst || !del)
return ;
while (*lst)
{
buffer = (**lst).next;
del((**lst).content);
free(*lst);
// '*lst' goes back to while loops condition and it will iterate this until
// all nodes are freed and contents are deleted
*lst = buffer;
}
*lst = NULL;
}
REMARK
This part in code above can be replaced by ft_lstdelone!
del((**lst).content);
free(lst);
*/