-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
57 lines (52 loc) · 1.63 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hshawand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 12:00:29 by hshawand #+# #+# */
/* Updated: 2019/04/19 12:44:02 by hshawand ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
#include <string.h>
static t_list *ft_clean(t_list *failed_list)
{
t_list *temp;
while (failed_list)
{
temp = failed_list;
failed_list = failed_list->next;
free(temp->content);
free(temp);
}
return (NULL);
}
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
{
t_list *start;
t_list *current;
t_list *tmp;
if (!lst || !f)
return (NULL);
start = (t_list *)malloc(sizeof(t_list));
if (!start)
return (NULL);
current = start;
while (lst)
{
tmp = (*f)(lst);
if (!tmp)
return (ft_clean(start));
*current = *tmp;
free(tmp);
current->next = (t_list *)malloc(sizeof(t_list));
if (!current->next)
return (ft_clean(start));
lst = lst->next;
current = current->next;
}
return (start);
}