-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
37 lines (33 loc) · 1.52 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: mbutt <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/24 22:17:21 by mbutt #+# #+# */
/* Updated: 2019/03/25 13:24:07 by mbutt ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Description - Iterates a list lst and applies the function f to each link to
** create a “fresh” list (using malloc(3)) resulting from the suc- cessive
** applications of f. If the allocation fails, the function returns NULL.
** Param#1 - A pointer to the first link of a list.
** Param#2 - The address of a function to apply to each link of a list.
** Return Value - The new list.
** Libc function - malloc, free.
*/
#include "libft.h"
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *new_list;
if (!lst)
return (NULL);
new_list = f(lst);
if (lst)
{
new_list->next = ft_lstmap(lst->next, f);
}
return (new_list);
}