lstdelone est ecrit avec un main
This commit is contained in:
Binary file not shown.
BIN
srcs/bonus/a.out
BIN
srcs/bonus/a.out
Binary file not shown.
@@ -1,95 +1,108 @@
|
|||||||
|
/*
|
||||||
|
** free an element and delete its content with del
|
||||||
|
** next is not free
|
||||||
|
*/
|
||||||
|
|
||||||
#include <libc.h>
|
/*
|
||||||
|
** #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 ft_delete(void *element)
|
||||||
|
** {
|
||||||
|
** *(char*)element = '\0';
|
||||||
|
** }
|
||||||
|
**
|
||||||
|
** void ft_lstdelone(t_list *lst, void (*del)(void *));
|
||||||
|
**
|
||||||
|
** int main(void)
|
||||||
|
** {
|
||||||
|
** char tresor;
|
||||||
|
** char matos;
|
||||||
|
** char friends;
|
||||||
|
** t_list *toto;
|
||||||
|
** t_list *tmp;
|
||||||
|
** void (ft_delete)(void*);
|
||||||
|
**
|
||||||
|
** tresor = 'a';
|
||||||
|
** matos = 'b';
|
||||||
|
** friends = 'c';
|
||||||
|
** toto = ft_lstnew(&tresor);
|
||||||
|
** tmp = ft_lstnew(&matos);
|
||||||
|
** ft_lstadd_back(&toto, tmp);
|
||||||
|
** tmp = ft_lstnew(&friends);
|
||||||
|
** ft_lstadd_back(&toto, tmp);
|
||||||
|
** 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));
|
||||||
|
** ft_lstdelone(toto->next, 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);
|
||||||
|
** }
|
||||||
|
*/
|
||||||
|
|
||||||
typedef struct s_list
|
#include "libft.h"
|
||||||
{
|
|
||||||
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 ft_lstdelone(t_list *lst, void (*del)(void *));
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
char tresor;
|
|
||||||
char matos;
|
|
||||||
char friends;
|
|
||||||
t_list *toto;
|
|
||||||
t_list *tmp;
|
|
||||||
|
|
||||||
tresor = 'a';
|
|
||||||
matos = 'b';
|
|
||||||
friends = 'c';
|
|
||||||
toto = ft_lstnew(&tresor);
|
|
||||||
printf("toto->data :%c\n",*(char*)(toto->content));
|
|
||||||
tmp = ft_lstnew(&matos);
|
|
||||||
ft_lstadd_back(&toto, tmp);
|
|
||||||
printf("----------------------\n");
|
|
||||||
printf("toto->data :%c\n",*(char*)(toto->content));
|
|
||||||
printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
|
|
||||||
tmp = ft_lstnew(&friends);
|
|
||||||
ft_lstadd_back(&toto, tmp);
|
|
||||||
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"
|
|
||||||
|
|
||||||
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user