-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstclear_bonus.c
44 lines (40 loc) · 2.05 KB
/
ft_lstclear_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oadewumi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 16:16:21 by oadewumi #+# #+# */
/* Updated: 2023/12/13 11:37:56 by oadewumi ### ########.fr */
/* */
/* ************************************************************************** */
/* In this function, A double pointer is used to modify the original pointer
to the list. Since the function is intended to clear the entire list and
modify the pointer to an empty list, it needs to change the value of the
original pointer passed to it, which requires a double pointer. */
/* t_list **lst: Represents a pointer to a pointer to the first node of the
linked list. It is a double pointer because the function will modify the
original pointer to the list.
void (*del)(void*): Is a function pointer that points to a function
responsible for freeing the content of each node. */
/* while (*lst) enters the loop until (*lst) becomes NULL (end of list). */
/* It iterates through the list, frees the memory allocated for each node,
deletes or frees the content of each node using the provided deletion function,
and updates the pointers accordingly.
Finally, it sets the original pointer to the list to NULL, indicating an
empty list after clearing all its elements. */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
t_list *temp;
while (*lst)
{
temp = (*lst)->next;
if ((del))
(del)((*lst)->content);
free(*lst);
(*lst) = temp;
}
(*lst) = NULL;
}