-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
executable file
·58 lines (51 loc) · 1.88 KB
/
ft_lstmap.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jchoy-me <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/20 15:32:08 by jchoy-me #+# #+# */
/* Updated: 2023/07/21 14:26:54 by jchoy-me ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
DESCRIPTION:
Iterates the list ’lst’ and applies the function ’f’ on the content of each
node. Creates a new list resulting of the successive applications of the
function ’f’. The ’del’ function is used to delete the content of a node
if needed.
PARAMETERS:
lst: The address of a pointer to a node.
f: The address of the function used to iterate on the list.
del: The address of the function used to delete the content of a node
if needed.
RETURN VALUE:
The new list.
NULL if the allocation fails.
EXTERNAL FUNCTIONS:
malloc, free
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newlst;
t_list *newnode;
void *newdata;
if (lst == NULL || f == NULL || del == NULL)
return (NULL);
newlst = NULL;
while (lst != NULL)
{
newdata = f(lst->content);
newnode = ft_lstnew(newdata);
if (newnode == NULL)
{
ft_lstclear(&newlst, del);
del(newdata);
}
ft_lstadd_back(&newlst, newnode);
lst = lst->next;
}
return (newlst);
}