-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
40 lines (37 loc) · 1.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: arepsa <arepsa@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/24 12:08:32 by arepsa #+# #+# */
/* Updated: 2023/04/24 12:10:59 by arepsa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*Applies the function ’f’ on the content of each node of lst.
result is stored in a new list.
The ’del’ function is used to delete the content of a node if needed.*/
/*lst - address of a pointer to a node.
f - address of the function used to iterate on the list.
del - address of the function to delete the content of a node if needed.*/
/*Return the new list, NULL if allocation fails*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new_list;
t_list *element;
new_list = NULL;
while (lst)
{
element = ft_lstnew((*f)(lst->content));
if (!element)
{
ft_lstclear(&new_list, del);
return (NULL);
}
ft_lstadd_back(&new_list, element);
lst = lst->next;
}
return (new_list);
}