-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap.c
84 lines (77 loc) · 2.34 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elel-yak <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 21:47:47 by elel-yak #+# #+# */
/* Updated: 2022/10/27 14:23:12 by elel-yak ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *new;
t_list *tmp;
if (!(lst && f && del))
return (0);
new = ft_lstnew(f(lst->content));
if (!new)
return (0);
lst = lst->next;
while (lst)
{
tmp = ft_lstnew(f(lst->content));
if (!tmp)
{
ft_lstclear(&new, del);
return (0);
}
ft_lstadd_back(&new, tmp);
lst = lst->next;
}
return (new);
}
void *f(void *content)
{
if (content)
{
strcpy(content, "Hello");
}
return (content);
}
void del(void *elem)
{
if (elem)
{
elem = 0;
free(elem);
}
}
// int main(void)
// {
// char str1[15] = "Hello world";
// char str2[15] = "42";
// char str3[15] = "";
// char str4[15] = "Found it!";
// t_list *elem;
// t_list *new_elem;
// elem = ft_lstnew(str1);
// elem->next = ft_lstnew(str2);
// elem->next->next = ft_lstnew(str3);
// elem->next->next->next = ft_lstnew(str4);
// new_elem = ft_lstmap(elem, f, del);
// if (!strcmp(new_elem->content, "Hello") \
// && !strcmp(new_elem->next->content, "Hello") \
// && !strcmp(new_elem->next->next->content, "Hello") \
// && !strcmp(new_elem->next->next->next->content, "Hello") \
// && !strcmp(elem->content, str1) \
// && !strcmp(elem->next->content, str2) \
// && !strcmp(elem->next->next->content, str3) \
// && !strcmp(elem->next->next->next->content, str4))
// printf("\033[92mTest %2.i - OK \033[0m\n", 1);
// else
// printf("\033[91mTest %2.i - KO \033[0m\n", 1);
// return (0);
// }