-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap_bonus.c
117 lines (112 loc) · 2.47 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungkim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/02 11:48:22 by kyungkim #+# #+# */
/* Updated: 2024/12/02 17:02:58 by kyungkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *head;
t_list *node;
void *node_c;
head = 0;
if (!f || !del || !lst)
return (NULL);
while (lst)
{
node_c = f(lst->content);
node = ft_lstnew(node_c);
if (!node)
{
del(node_c);
ft_lstclear(&head, del);
return (NULL);
}
ft_lstadd_back(&head, node);
lst = lst->next;
}
return (head);
}
/*
static t_list *create_node(t_list *head, void (*del)(void *))
{
t_list *node;
node = (t_list *)malloc(sizeof(t_list));
if (!node)
{
ft_lstclear(&head, del);
return (NULL);
}
return (node);
}
*/
/*
node = create_node(head, del);
if (!node)
return (NULL);
if (!head)
head = node;
else
new->next = node;
new = node;
new->content = f(lst->content);
new->next = NULL;
lst = lst->next;
*/
/*
void *nextcha(void *content)
{
int i = 0;
while (((char *)content)[i])
{
((char *)content)[i]++;
i++;
}
return (content);
}
int main(void)
{
char arr[] = "hi";
t_list *start = ft_lstnew(arr);
printf("old %p %s\n", start, (char *)start->content);
t_list *new = ft_lstmap(start, nextcha, NULL);
printf("new %p %s", new, (char *)new->content);
}
*/
/*
if (new)
{
new->next = (t_list *)malloc(sizeof(t_list));
if (!new->next)
{
ft_lstclear(&head,del);
return (NULL);
}
new = new->next;
}
else
{
head = (t_list *)malloc(sizeof(t_list));
if (!head)
return (NULL);
new = head;
}
if (f)
new->content = f(lst->content);
lst = lst->next;
}
new->next = NULL;
*/
/*
if (!node)
{
ft_lstclear(&head, del);
return (NULL);
}
*/