-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lstmap.c
131 lines (117 loc) · 2.82 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
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
127
128
129
130
131
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmattera <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 13:51:45 by nmattera #+# #+# */
/* Updated: 2022/05/23 11:47:41 by nmattera ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *begin;
t_list *new;
if (!lst || !f || !del)
return (NULL);
begin = NULL;
while (lst)
{
new = ft_lstnew((*f)(lst->content));
if (!new)
{
ft_lstclear(&begin, del);
return (NULL);
}
if (!begin)
begin = new;
else
ft_lstadd_back(&begin, new);
lst = lst->next;
}
return (begin);
}
/***************************************************************/
/* #include <unistd.h>
#include <string.h>
#include <stdio.h>
void ft_print_result(t_list *elem)
{
int len;
len = 0;
while (((char *)elem->content)[len])
len++;
write(1, ((char *)elem->content), len);
write(1, "\n", 1);
}
t_list *ft_lstnewone(void *content)
{
t_list *elem;
elem = (t_list *)malloc(sizeof(t_list));
if (!elem)
return (NULL);
if (!content)
elem->content = NULL;
else
elem->content = content;
elem->next = NULL;
return (elem);
}
void *ft_map(void *ct)
{
int i;
void *c;
char *pouet;
c = ct;
i = -1;
pouet = (char *)c;
while (pouet[++i])
if (pouet[i] == 'o')
pouet[i] = 'a';
return (c);
}
void ft_del(void *content)
{
free(content);
}
int main(int argc, const char *argv[])
{
t_list *elem;
t_list *elem2;
t_list *elem3;
t_list *elem4;
t_list *list;
char *str = strdup("lorem");
char *str2 = strdup("ipsum");
char *str3 = strdup("dolor");
char *str4 = strdup("sit");
elem = ft_lstnewone(str);
elem2 = ft_lstnewone(str2);
elem3 = ft_lstnewone(str3);
elem4 = ft_lstnewone(str4);
alarm(5);
if (argc == 1 || !elem || !elem2 || !elem3 || !elem4)
return (0);
elem->next = elem2;
elem2->next = elem3;
elem3->next = elem4;
if (atoi(argv[1]) == 1)
{
if (!(list = ft_lstmap(elem, &ft_map, &ft_del)))
return (0);
if (list == elem)
write(1, "A new list is not returned\n", 27);
int i;
i = 0;
ft_print_result(list);
while (list->next)
{
list = list->next;
ft_print_result(list);
i++;
}
}
return (0);
} */