-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_lstmap.c
More file actions
66 lines (60 loc) · 1.9 KB
/
ft_lstmap.c
File metadata and controls
66 lines (60 loc) · 1.9 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sparth <sparth@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/19 15:17:25 by sparth #+# #+# */
/* Updated: 2023/10/19 19:36:56 by sparth ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *transform;
t_list *head;
t_list *ptr;
transform = lst;
head = NULL;
if (!lst || !f)
return (NULL);
while (transform)
{
ptr = ft_lstnew(f(transform->content));
if (!ptr)
{
ft_lstclear(&head, del);
return (NULL);
}
ft_lstadd_back(&head, ptr);
transform = transform->next;
}
return (head);
}
// void delete(void *content)
// {
// free(content);
// }
// void *overwrite(void *content)
// {
// ft_strdup("HAllO");
// }
// #include <stdio.h>
// int main()
// {
// char string1 [] = "Helll";
// char string2 [] = "belll";
// char string3 [] = "selll";
// t_list *head = ft_lstnew(string1);
// t_list *node1 = ft_lstnew(string2);
// t_list *node2 = ft_lstnew(string3);
// ft_lstadd_back(&head, node1);
// ft_lstadd_back(&head, node2);
// t_list *overwritten = ft_lstmap(head, delete, overwrite);
// while (overwritten)
// {
// printf("%s",overwritten->content);
// overwritten = overwritten->next;
// }
// }