forked from josanri/Libft_extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
46 lines (43 loc) · 1.69 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aalvarez <aalvarez@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/17 22:58:18 by aalvarez #+# #+# */
/* Updated: 2022/08/17 23:13:47 by aalvarez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* @brief iterates the list pointed to by lst and applies the function
* f to the content of every node. Creates a new list resulting of the
* sucessive apllications of the function f. The del function is used to
* delete the content of a node if needed.
*
* @param lst the address of a pointer to a node.
* @param f the address of the function used to iterate on the list.
* @param del the address of the function used to delete.
* @return t_list* the new list.
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *head;
t_list *new;
if (!lst || !f)
return (NULL);
head = NULL;
while (lst)
{
new = ft_lstnew((*f)(lst->content));
if (!new)
{
ft_lstclear(&head, del);
return (NULL);
}
ft_lstadd_back(&head, new);
lst = lst->next;
}
return (head);
}