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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/22 02:02:24 by cacharle #+# #+# */
/* Updated: 2020/04/12 19:51:04 by charles ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_list
{
void *data;
struct s_list *next;
} t_list;
int*
create_data_elem(int data)
{
int *data_ptr = malloc(sizeof(int));
if (data_ptr == NULL)
return (NULL);
*data_ptr = data;
return data_ptr;
}
t_list*
create_elem(int data)
{
t_list *new = malloc(sizeof(t_list));
if (new == NULL)
return NULL;
if ((new->data = create_data_elem(data)) == NULL)
return (NULL);
new->next = NULL;
return new;
}
static void
push_front(t_list **lst_ptr, t_list *new_front)
{
if (lst_ptr == NULL || new_front == NULL)
return ;
new_front->next = *lst_ptr;
*lst_ptr = new_front;
}
static t_list*
reverse(t_list *list)
{
if (list == NULL || list->next == NULL)
return list;
t_list *tmp = reverse(list->next);
list->next->next = list;
list->next = NULL;
return tmp;
}
t_list*
list_from_format(char *fmt)
{
t_list *head = NULL;
while (fmt != NULL && *fmt)
{
int n = (int)strtol(fmt, &fmt, 10);
t_list *elem = create_elem(n);
push_front(&head, elem);
}
return reverse(head);
}
t_list*
list_dup(t_list *list)
{
t_list *dup_head = NULL;
while (list != NULL)
{
t_list *tmp = create_elem(*(int*)list->data);
push_front(&dup_head, tmp);
}
return reverse(dup_head);
}
int
list_cmp(t_list *l1, t_list *l2)
{
if (l1 == NULL && l2 == NULL)
return 0;
if (l1 == NULL)
return -1;
if (l2 == NULL)
return 1;
if (l1->data == NULL)
return -1;
if (l2->data == NULL)
return 1;
if (*(int*)l1->data != *(int*)l2->data)
return *(int*)l1->data - *(int*)l2->data;
return list_cmp(l1->next, l2->next);
}
void
list_print(t_list *list)
{
while (list != NULL)
{
printf("[%d] -> ", *(int*)list->data);
list = list->next;
}
printf("(null)");
}
void
list_destroy(t_list *list)
{
if (list == NULL)
return ;
list_destroy(list->next);
if (list->data != NULL)
free(list->data);
free(list);
}
int ft_strlen(char *);
char *ft_strcpy(char *dst, const char *src);
int ft_strcmp(const char *s1, const char *s2);
int ft_write(int, const void*, size_t);
int ft_read(int, void*, size_t);
char *ft_strdup(const char*);
int ft_atoi_base(const char*, const char*);
void ft_list_push_front(t_list **begin_list, void *data);
int ft_list_size(t_list *begin_list);
void ft_list_sort(t_list **begin_list, int (*cmp)());
void ft_list_remove_if(t_list **begin_list, void *data_ref, int (*cmp)(), void (*free_fct)(void*));
int compar(void *a, void *b)
{
printf("---\n");
printf("a = %p, b = %p\n", a, b);
printf("*a = %d, *b = %d\n", *(int*)a, *(int*)b);
int c = *(int*)a - *(int*)b;
printf("ret %d\n", c);
return c;
}
void free_fct(void *data)
{
printf("free: %p\n", data);
printf("free data %d\n", *(int*)data);
free(data);
printf("end free\n");
fflush(stdout);
}
int main()
{
/* char *a = ""; */
/* char *b = "a"; */
/* printf("%d\n", sizeof(char*)); */
/* printf("a: %p\n", (void*)a); */
/* printf("b: %p\n", (void*)a); */
/* printf("%d\n", ft_strlen(a)); */
/* printf("%d\n", ft_strlen(b)); */
/* printf("%d\n", ft_strlen("bonjour")); */
/* */
/* char c[32] = "bon"; */
/* char *d = "b"; */
/* */
/* printf("%s\n", c); */
/* printf("%s\n", ft_strcpy(c, d)); */
/* printf("%s\n", c); */
/* char *e = "\x01"; */
/* char *f = "\x02"; */
/* printf("%d\n", ft_strcmp(e, f)); */
/* printf("%d\n", strcmp(e, f)); */
/* ft_write(1, "bon\n", 4); */
/* */
/* char g[32]; */
/* int ret = ft_read(0, g, 2); */
/* g[ret] = 0; */
/* printf("%s\n", g); */
/* char *h = ft_strdup("bonjour"); */
/* printf("%d\n", h); */
/* printf("%s\n", h); */
/* free(h); */
/* printf("%d\n", check_base("01")); */
/* printf("%d\n", ft_atoi_base(" \t\n\r\v\f\r 10\t0101001", "01")); */
/* printf("_%d_\n", ft_atoi_base(" 755x+", "01234567")); */
/* system("vmmap a.out"); */
/* t_list *a = list_from_format("-1 2 3 98 1234 12 123 4 123 -123 2 5 6 0 1 3 4"); */
/* t_list *a = list_from_format("1"); */
/* t_list *a = NULL; */
/* printf("%d: ", ft_list_size(a)); list_print(a); putchar('\n'); */
/* ft_list_sort(&a, compar); */
/* printf("%d: ", ft_list_size(a)); list_print(a); putchar('\n'); */
t_list *a = list_from_format("1 2 3 4 1 2 3 1");
int data_ref = 1;
printf("f: %p\n", a->data);
/* free(a->data); */
printf("%p\n", &data_ref);
list_print(a); putchar('\n');
ft_list_remove_if(&a, &data_ref, compar, free_fct);
list_print(a); putchar('\n');
list_destroy(a);
return 0;
}
|