-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstmap_bonus.c
126 lines (111 loc) · 3.22 KB
/
ft_lstmap_bonus.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edetoh <edetoh@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 11:33:24 by edetoh #+# #+# */
/* Updated: 2024/10/25 11:13:23 by edetoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
ft_lstmap applies 'f' to each element of 'lst' and creates a new list.
and creates a new list.
Takes 'lst' (linked list), 'f' (function to apply)
and 'del' (function to free contents).
Returns the new list, or NULL on failure.
*/
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *newlist;
t_list *temp_next;
newlist = NULL;
while (lst)
{
temp_next = ft_lstnew(NULL);
if (!temp_next)
{
ft_lstclear(&newlist, del);
break ;
}
temp_next->content = f(lst->content);
ft_lstadd_back(&newlist, temp_next);
lst = lst->next;
}
return (newlist);
}
// #include <stdio.h>
// #include <stdlib.h>
// #include <string.h>
// void delcontent(void *content)
// {
// free(content);
// }
// void *transform(void *content)
// {
// char *new_content = ft_strdup("Mohahah 😈 ");
// if (content)
// {
// printf("str : %s\n", (const char *)content);
// printf("new cont : %s\n", (const char *)new_content);
// return (ft_strjoin((const char *)new_content, (const char *)content));
// }
// return (NULL);
// }
// int main(void)
// {
// t_list *node;
// char *content = ft_strdup("bonjour !");
// node = ft_lstnew(content);
// if (node)
// {
// t_list *newbuble1;
// char *newbuble1_content = ft_strdup("coucou !");
// newbuble1 = ft_lstnew(newbuble1_content);
// if (newbuble1)
// {
// ft_lstadd_back(&node, newbuble1);
// t_list *newbuble2;
// char *newbuble2_content = ft_strdup("salut !");
// newbuble2 = ft_lstnew(newbuble2_content);
// if (newbuble2)
// {
// ft_lstadd_back(&node, newbuble2);
// // Utiliser ft_lstmap pour transformer la liste
// t_list *mapped_list = ft_lstmap(node, transform, delcontent);
// if (mapped_list)
// {
// t_list *temp = mapped_list;
// while (temp)
// {
// printf("Mapped content: %s\n", (char *)temp->content);
// temp = temp->next;
// }
// ft_lstclear(&mapped_list, delcontent);
// }
// else
// {
// printf("Failed to map list\n");
// }
// }
// else
// {
// printf("Failed to create newbuble2\n");
// }
// }
// else
// {
// printf("Failed to create newbuble1\n");
// }
// }
// else
// {
// printf("Failed to create node\n");
// }
// // Libérer la mémoire allouée
// if (node)
// ft_lstclear(&node, delcontent);
// return 0;
// }