-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstclear.c
54 lines (44 loc) · 2.07 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mbrito-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/08 20:25:51 by mbrito-p #+# #+# */
/* Updated: 2023/05/08 20:25:51 by mbrito-p ### ########.fr */
/* */
/* ************************************************************************** */
// 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.
// lst: The address of a pointer to a node.
// del: The address of the function used to delete
// the content of the node.
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void*))
{
// Declare two pointers to t_list
t_list *upcomming;
t_list *current;
// Check if lst is NULL or the pointer to the list is NULL
if (!lst || !*lst)
return;
// Set current to the first element in the list
current = *lst;
// Loop through the list until the end is reached
while (current)
{
// Store the pointer to the next element in the list
upcomming = current->next;
// Call the del function on the content of the current element
del(current->content);
// Free the memory allocated for the current element
free(current);
// Move to the next element in the list
current = upcomming;
}
// Set the pointer to the list to NULL to prevent a dangling pointer
*lst = NULL;
}