-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
37 lines (33 loc) · 1.38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mgiraldo <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/04 11:57:24 by mgiraldo #+# #+# */
/* Updated: 2020/03/04 15:54:15 by mgiraldo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Iterates a list lst and applies the function f to each link to create a
** “fresh” list, resulting from the successive applications of f. If the
** allocation fails, the function returns NULL.
*/
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *head;
t_list *iter;
if (!lst || !(iter = ft_lstnew(NULL, 0)))
return (NULL);
iter = f(lst);
head = iter;
while (lst->next)
{
lst = lst->next;
iter->next = f(lst);
iter = iter->next;
}
return (head);
}