lstmap fonctionne
This commit is contained in:
@@ -16,7 +16,9 @@
|
||||
*/
|
||||
|
||||
/*
|
||||
** #include <libc.h>
|
||||
** #include <stdlib.h>
|
||||
** #include <unistd.h>
|
||||
** #include <stdio.h>
|
||||
**
|
||||
** typedef struct s_list
|
||||
** {
|
||||
@@ -57,10 +59,29 @@
|
||||
** }
|
||||
** }
|
||||
**
|
||||
** char *ft_strdup(const char *src)
|
||||
** {
|
||||
** int i;
|
||||
** char *str;
|
||||
**
|
||||
** i = 0;
|
||||
** while (src[i] != '\0')
|
||||
** i++;
|
||||
** if (!(str = (char*)malloc(sizeof(*str) * (i + 1))))
|
||||
** return (NULL);
|
||||
** while (i-- >= 0)
|
||||
** str[i + 1] = src[i + 1];
|
||||
** return (str);
|
||||
** }
|
||||
**
|
||||
** void *to_uppercase(void *element)
|
||||
** {
|
||||
** *(char*)(((t_list*)element)->content) -= 32;
|
||||
** return (element);
|
||||
** char *i;
|
||||
**
|
||||
** if (!(i = ft_strdup((char*)element)))
|
||||
** return (NULL);
|
||||
** *i -= 32;
|
||||
** return ((void *)i);
|
||||
** }
|
||||
**
|
||||
** void ft_delete(void *element)
|
||||
@@ -76,18 +97,18 @@
|
||||
** void *(to_uppercase)(void *);
|
||||
** void (ft_delete)(void*);
|
||||
**
|
||||
** toto = ft_lstnew("a");
|
||||
** toto = ft_lstnew("aa");
|
||||
** toto->next = ft_lstnew("b");
|
||||
** toto->next->next = ft_lstnew("c");
|
||||
** printf("toto->data :%c\n",*(char*)(toto->content));
|
||||
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
|
||||
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
|
||||
** printf("toto->data :%s\n",(char*)(toto->content));
|
||||
** printf("toto->nxt->data :%s\n",(char*)(toto->next->content));
|
||||
** printf("toto->nxt->nxt->data:%s\n",(char*)(toto->next->next->content));
|
||||
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
|
||||
** ft_lstmap(toto->next, to_uppercase, ft_delete);
|
||||
** toto = ft_lstmap(toto, to_uppercase, ft_delete);
|
||||
** printf("----------------------\n");
|
||||
** printf("toto->data :%c\n",*(char*)(toto->content));
|
||||
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
|
||||
** printf("toto->nxt->nxt->data:%c\n",*(char*)(toto->next->next->content));
|
||||
** printf("toto->data :%s\n",(char*)(toto->content));
|
||||
** printf("toto->nxt->data :%s\n",(char*)(toto->next->content));
|
||||
** printf("toto->nxt->nxt->data:%s\n",(char*)(toto->next->next->content));
|
||||
** printf("toto->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
|
||||
** return (0);
|
||||
** }
|
||||
@@ -100,14 +121,17 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
||||
t_list *new;
|
||||
t_list *tmp;
|
||||
|
||||
if (!lst)
|
||||
if(!(tmp = ft_lstnew(f(lst->content))))
|
||||
{
|
||||
del(tmp->content);
|
||||
free(tmp);
|
||||
return (NULL);
|
||||
tmp = (t_list*)f(lst);
|
||||
}
|
||||
new = tmp;
|
||||
while (lst->next)
|
||||
{
|
||||
lst = lst->next;
|
||||
if (!(tmp->next = (t_list*)f(lst)))
|
||||
if(!(tmp->next = ft_lstnew(f(lst->content))))
|
||||
{
|
||||
del(tmp->next->content);
|
||||
free(tmp->next);
|
||||
|
||||
Reference in New Issue
Block a user