correction lstnew erreur de malloc
This commit is contained in:
BIN
.Makefile.swn
BIN
.Makefile.swn
Binary file not shown.
BIN
srcs/.DS_Store
vendored
Normal file
BIN
srcs/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
srcs/bonus/a.out
BIN
srcs/bonus/a.out
Binary file not shown.
99
srcs/bonus/ft_dl.c
Normal file
99
srcs/bonus/ft_dl.c
Normal file
@@ -0,0 +1,99 @@
|
||||
|
||||
#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)
|
||||
{
|
||||
t_list *toto;
|
||||
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_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);
|
||||
}
|
||||
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||
{
|
||||
del(lst->content);
|
||||
free(lst);
|
||||
lst = NULL;
|
||||
}
|
||||
108
srcs/bonus/ft_lstclear.c
Normal file
108
srcs/bonus/ft_lstclear.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
** delete and free an element of the list and all the followings
|
||||
*/
|
||||
|
||||
/*
|
||||
** #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 *))
|
||||
** {
|
||||
** del(lst->content);
|
||||
** free(lst);
|
||||
** lst = NULL;
|
||||
** }
|
||||
**
|
||||
** void ft_lstclear(t_list **lst, void (*del)(void *));
|
||||
**
|
||||
** int main(void)
|
||||
** {
|
||||
** t_list *toto;
|
||||
** 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_lstclear(&(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 :%s\n",(char*)(toto->next->next));
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstclear(t_list **lst, void (*del)(void *))
|
||||
{
|
||||
if ((*lst)->next)
|
||||
ft_lstclear(&(*lst)->next, del);
|
||||
ft_lstdelone(*lst, del);
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@
|
||||
** }
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
//#include "libft.h"
|
||||
|
||||
void ft_lstdelone(t_list *lst, void (*del)(void *))
|
||||
{
|
||||
|
||||
124
srcs/bonus/ft_lstmap.c
Normal file
124
srcs/bonus/ft_lstmap.c
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
** 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)))
|
||||
{
|
||||
free(tmp->next);
|
||||
return (NULL);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
@@ -33,13 +33,13 @@
|
||||
** t_list *toto;
|
||||
**
|
||||
** tresor = 'd';
|
||||
** printf("tresor : %c\n",tresor);
|
||||
** printf("tresor : %c\n",tresor);
|
||||
** toto = ft_lstnew(&tresor);
|
||||
** //toto->content was alocated as void* so it need cast
|
||||
** printf("toto->content : %c\n",*(char*)(toto->content));
|
||||
** printf("toto->content : %c\n",*(char*)(toto->content));
|
||||
** tresor = 'D';
|
||||
** printf("transform tresor : %c\n",tresor);
|
||||
** printf("but not toto->content: %c\n",*(char*)(toto->content));
|
||||
** printf("transform tresor : %c\n",tresor);
|
||||
** printf("and also toto->content: %c\n",*(char*)(toto->content));
|
||||
** return (0);
|
||||
** }
|
||||
*/
|
||||
@@ -55,11 +55,7 @@ t_list *ft_lstnew(void *content)
|
||||
if (!content)
|
||||
lst->content = NULL;
|
||||
else
|
||||
{
|
||||
if (!(lst->content = malloc(sizeof(content))))
|
||||
return (NULL);
|
||||
ft_memcpy(lst->content, content, sizeof(content));
|
||||
}
|
||||
lst->content = content;
|
||||
lst->next = NULL;
|
||||
return (lst);
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#include "libft.h"
|
||||
|
||||
void ft_lstdel(t_list **alst, void (*del)(void *, size_t))
|
||||
{
|
||||
if ((*alst)->next)
|
||||
ft_lstdel(&(*alst)->next, del);
|
||||
ft_lstdelone(alst, del);
|
||||
}
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstmap.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/11/14 21:11:20 by hulamy #+# #+# */
|
||||
/* Updated: 2018/11/16 14:01:23 by hulamy ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
t_list *ft_lstmap(t_list *lst, t_list *(*f)(t_list *elem))
|
||||
{
|
||||
t_list *new;
|
||||
t_list *tmp;
|
||||
|
||||
if (!lst)
|
||||
return (NULL);
|
||||
tmp = f(lst);
|
||||
new = tmp;
|
||||
while (lst->next)
|
||||
{
|
||||
lst = lst->next;
|
||||
if (!(tmp->next = f(lst)))
|
||||
{
|
||||
free(tmp->next);
|
||||
return (NULL);
|
||||
}
|
||||
tmp = tmp->next;
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
Reference in New Issue
Block a user