126 lines
2.7 KiB
C
126 lines
2.7 KiB
C
/*
|
|
** iterate trhough linked list and apply to each element a function f
|
|
** if necessary the function del is used to delete an element
|
|
*/
|
|
|
|
/*
|
|
** #include <libc.h>
|
|
**
|
|
** typedef struct s_list
|
|
** {
|
|
** void *content;
|
|
** struct s_list *next;
|
|
** } t_list;
|
|
**
|
|
** void *ft_memcpy(void *dst, const void *src, size_t n)
|
|
** {
|
|
** size_t i;
|
|
** char *ptr;
|
|
** char *ptr2;
|
|
**
|
|
** ptr = (char *)dst;
|
|
** ptr2 = (char *)src;
|
|
** i = -1;
|
|
** while (++i < n)
|
|
** ptr[i] = ptr2[i];
|
|
** return (dst);
|
|
** }
|
|
**
|
|
** t_list *ft_lstnew(void *content)
|
|
** {
|
|
** t_list *lst;
|
|
**
|
|
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
|
|
** return (NULL);
|
|
** if (!content)
|
|
** lst->content = NULL;
|
|
** else
|
|
** {
|
|
** if (!(lst->content = malloc(sizeof(content))))
|
|
** return (NULL);
|
|
** ft_memcpy(lst->content, content, sizeof(content));
|
|
** }
|
|
** lst->next = NULL;
|
|
** return (lst);
|
|
** }
|
|
**
|
|
** void ft_lstadd_back(t_list **alst, t_list *new)
|
|
** {
|
|
** t_list *tmp;
|
|
**
|
|
** if (alst)
|
|
** {
|
|
** tmp = *alst;
|
|
** if (!tmp)
|
|
** *alst = new;
|
|
** else
|
|
** {
|
|
** while (tmp->next)
|
|
** tmp = tmp->next;
|
|
** tmp->next = new;
|
|
** }
|
|
** new->next = NULL;
|
|
** }
|
|
** }
|
|
**
|
|
** void *to_uppercase(void *element)
|
|
** {
|
|
** *(char*)(((t_list*)element)->content) -= 32;
|
|
** return (element);
|
|
** }
|
|
**
|
|
** void ft_delete(void *element)
|
|
** {
|
|
** *(char*)element = '\0';
|
|
** }
|
|
**
|
|
** t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
|
|
**
|
|
** int main(void)
|
|
** {
|
|
** t_list *toto;
|
|
** void *(to_uppercase)(void *);
|
|
** void (ft_delete)(void*);
|
|
**
|
|
** toto = ft_lstnew("a");
|
|
** 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->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
|
|
** ft_lstmap(toto->next, 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->nxt->nxt->nxt :%s\n",(char*)(toto->next->next->next));
|
|
** return (0);
|
|
** }
|
|
*/
|
|
|
|
#include "libft.h"
|
|
|
|
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
|
|
{
|
|
t_list *new;
|
|
t_list *tmp;
|
|
|
|
if (!lst)
|
|
return (NULL);
|
|
tmp = (t_list*)f(lst);
|
|
new = tmp;
|
|
while (lst->next)
|
|
{
|
|
lst = lst->next;
|
|
if (!(tmp->next = (t_list*)f(lst)))
|
|
{
|
|
del(tmp->next->content);
|
|
free(tmp->next);
|
|
return (NULL);
|
|
}
|
|
tmp = tmp->next;
|
|
}
|
|
return (new);
|
|
}
|