reorganisation pour rendu et free dans split

This commit is contained in:
Hugo LAMY
2019-12-02 17:50:04 +01:00
parent 88ac279f99
commit 90106ef3ae
75 changed files with 47 additions and 40 deletions

View File

@@ -1,91 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:11:53 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:12 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the end of a list
** or first if list has no element so far
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_back(t_list **alst, t_list *new);
**
** 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_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;
}
}

View File

@@ -1,94 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:12:02 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:54 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the begining of a list
*/
/*
** #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_front(t_list **alst, t_list *new);
**
** 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_front(&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_front(&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->dqta:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
void ft_lstadd_front(t_list **alst, t_list *new)
{
new->next = *alst;
*alst = new;
}

View File

@@ -1,107 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:13:30 by hulamy #+# #+# */
/* Updated: 2019/11/28 17:06:48 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** 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;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = 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*);
**
** printf("sizeof(t_list)%lu\n",sizeof(t_list));
** 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 *))
{
t_list *next;
while (*lst != NULL)
{
next = (*lst)->next;
ft_lstdelone(*lst, del);
*lst = next;
}
}

View File

@@ -1,96 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:03 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:35:53 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** free an element and delete its content with del
** next is not free
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = 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);
** }
*/
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
del(lst->content);
free(lst);
lst = NULL;
}

View File

@@ -1,85 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:11 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:03:40 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** go through all elements of the list and apply the function f to each of them
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstiter(t_list *lst, void (*f)(void*));
**
** void to_uppercase(void *element)
** {
** // *(char*)(((t_list*)element)->content) -= 32;
** // or :
** t_list *tmp;
**
** tmp = (t_list*)element;
** *(char*)(tmp->content) -= 32;
** }
**
** int main(void)
** {
** t_list *toto;
** void to_uppercase(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));
** printf("---------------------------\n");
** ft_lstiter(toto, to_uppercase);
** 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_lstiter(t_list *lst, void (*f)(void *))
{
if (!f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

View File

@@ -1,100 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:14:49 by hulamy #+# #+# */
/* Updated: 2019/11/28 16:43:18 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return a pointer to the last element of a list
*/
/*
** #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_front(t_list **alst, t_list *new)
** {
** new->next = *alst;
** *alst = new;
** }
**
** t_list *ft_lstlast(t_list *lst);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** toto = ft_lstnew(&tresor);
** tresor = 'b';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** tresor = 'c';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** 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));
** tmp = ft_lstlast(toto);
** printf("%c\n",*(char*)(tmp->content));
** 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));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (lst)
while (lst->next)
lst = lst->next;
return (lst);
}

View File

@@ -1,145 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:15:42 by hulamy #+# #+# */
/* Updated: 2019/12/01 16:02:13 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** iterate trhough linked list and apply to each element a function f
** if necessary the function del is used to delete an element
*/
/*
** #include <stdlib.h>
** #include <unistd.h>
** #include <stdio.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = 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;
** }
** }
**
** 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 *i;
**
** if (!(i = ft_strdup((char*)element)))
** return (NULL);
** *i -= 32;
** return ((void *)i);
** }
**
** 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("aa");
** toto->next = ft_lstnew("b");
** toto->next->next = ft_lstnew("c");
** 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));
** toto = ft_lstmap(toto, to_uppercase, ft_delete);
** printf("----------------------\n");
** 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);
** }
*/
#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);
if (!(tmp = ft_lstnew(f(lst->content))))
{
del(tmp->content);
free(tmp);
return (NULL);
}
new = tmp;
while (lst->next)
{
lst = lst->next;
if (!(tmp->next = ft_lstnew(f(lst->content))))
{
del(tmp->next->content);
free(tmp->next);
return (NULL);
}
tmp = tmp->next;
}
return (new);
}

View File

@@ -1,59 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:16:20 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:29:46 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
**
** tresor = 'd';
** 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));
** tresor = 'D';
** printf("transform tresor : %c\n",tresor);
** printf("and also toto->content: %c\n",*(char*)(toto->content));
** return (0);
** }
*/
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *lst;
if (!(lst = (t_list *)malloc(sizeof(*lst))))
return (NULL);
if (!content)
lst->content = NULL;
else
lst->content = content;
lst->next = NULL;
return (lst);
}

View File

@@ -1,86 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:31:48 by hulamy #+# #+# */
/* Updated: 2019/11/25 16:06:41 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** return the size of the linked list
*/
/*
** #include <libc.h>
**
** typedef struct s_list
** {
** void *content;
** struct s_list *next;
** } t_list;
**
** t_list *ft_lstnew(void *content)
** {
** t_list *lst;
**
** if (!(lst = (t_list *)malloc(sizeof(*lst))))
** return (NULL);
** if (!content)
** lst->content = NULL;
** else
** lst->content = content;
** lst->next = NULL;
** return (lst);
** }
**
** void ft_lstadd_front(t_list **alst, t_list *new)
** {
** new->next = *alst;
** *alst = new;
** }
**
** int ft_lstsize(t_list *lst);
**
** int main(void)
** {
** char tresor;
** t_list *toto;
** t_list *tmp;
**
** tresor = 'a';
** toto = ft_lstnew(&tresor);
** tresor = 'b';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** tresor = 'c';
** tmp = ft_lstnew(&tresor);
** ft_lstadd_front(&toto, tmp);
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** printf("%i\n",ft_lstsize(toto));
** printf("toto->data :%c\n",*(char*)(toto->content));
** printf("toto->nxt->data :%c\n",*(char*)(toto->next->content));
** printf("toto->nxt->nxt->dqta:%c\n",*(char*)(toto->next->next->content));
** return (0);
** }
*/
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int size;
size = 0;
while (lst)
{
size++;
lst = lst->next;
}
return (size);
}