change fonctions pour list avec nouvelles fonctions pour listes a deux sens

This commit is contained in:
hugogogo
2022-03-24 16:49:59 +01:00
parent bc53060626
commit 60af4e44b0
27 changed files with 484 additions and 13 deletions

View File

@@ -62,15 +62,19 @@ SRCS = ft_memset.c \
ft_putendl_fd.c \
ft_putnbr_fd.c \
\
ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
ft_lstmap.c \
ft_lstcreate.c \
ft_lstpush_back.c \
ft_lstpush_front.c \
ft_lstloop.c \
ft_lstloop_back.c \
ft_lstbegin.c \
ft_lstend.c \
ft_lstfind.c \
ft_lstinsert.c \
ft_lsterase.c \
ft_lstfree.c \
ft_lstlen.c \
ft_lstcopy.c \
\
ft_strcat.c \
ft_strcmp.c \
@@ -120,8 +124,19 @@ SRCS = ft_memset.c \
ft_sqrt.c \
ft_free_tab.c \
\
ft_arrintchr.c \
ft_lstremove_next.c
ft_arrintchr.c
# ft_lstremove.c \
ft_lstfind.c
# ft_lstnew.c \
ft_lstadd_front.c \
ft_lstsize.c \
ft_lstlast.c \
ft_lstadd_back.c \
ft_lstdelone.c \
ft_lstclear.c \
ft_lstiter.c \
ft_lstmap.c
ODIR = ./builds

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:45:53 by hulamy #+# #+# */
/* Updated: 2022/03/23 21:51:14 by simplonco ### ########.fr */
/* Updated: 2022/03/24 16:38:25 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
@@ -60,9 +60,11 @@ void ft_putnbr_fd(int n, int fd);
typedef struct s_list
{
void *content;
struct s_list *prev;
struct s_list *next;
} t_list;
/*
t_list *ft_lstnew(void *content);
void ft_lstadd_front(t_list **alst, t_list *n);
void *ft_lstadd_back(t_list **alst, t_list *n);
@@ -72,6 +74,22 @@ void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstmap(t_list *l, void *(*f)(void*), void (*d)(void*));
*/
//void ft_lstremove(t_list *lst, void (*del)(void *));
//t_list *ft_lstfind(t_list *lst, void *to_find, int (*comp)(void*, void *));
t_list *ft_lstcreate(void *content);
void *ft_lstpush_back(t_list **lst, t_list *new);
void ft_lstpush_front(t_list **alst, t_list *new);
void ft_lstloop(t_list *lst, void (*f)(void *));
void ft_lstloop_back(t_list *lst, void (*f)(void *));
t_list *ft_lstbegin(t_list *lst);
t_list *ft_lstend(t_list *lst);
t_list *ft_lstfind(t_list *lst, void *to_find, int (*comp)(void*, void *));
void ft_lstinsert(t_list *lst, t_list *new);
void ft_lsterase(t_list *lst, void (*del)(void *));
void ft_lstfree(t_list *lst, void (*del)(void *));
int ft_lstlen(t_list *lst);
t_list *ft_lstcopy(t_list *lst, void *(*cpy)(void *));
char *ft_strcat(char *s1, const char *s2);
int ft_strcmp(const char *s1, const char *s2);
@@ -114,6 +132,5 @@ int ft_sqrt(int i);
void ft_free_tab(char **tab);
int ft_arrintchr(int * intarr, int comp, size_t size);
void ft_lstremove_next(t_list *lst, void (*del)(void *));
#endif

24
srcs/ft_lst_find.bak Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstfind.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/23 22:48:47 by simplonco #+# #+# */
/* Updated: 2022/03/23 22:49:16 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* find an element with the comp function, and return a pointer to it
*/
#include "libft.h"
t_list *ft_lstfind(t_list *lst, void *to_find, int (*comp)(void*, void *))
{
while (lst && (!comp(to_find, lst->content)))
lst = lst->next;
return (lst);
}

25
srcs/ft_lstbegin.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstbegin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:05:21 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:10:45 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* return a pointer to the first element of a two-way list
*/
#include "libft.h"
t_list *ft_lstbegin(t_list *lst)
{
while (lst->prev)
lst = lst->prev;
return (lst);
}

34
srcs/ft_lstcopy.c Normal file
View File

@@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstcopy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 16:26:27 by simplonco #+# #+# */
/* Updated: 2022/03/24 16:37:39 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* copy a two-way list with the copy function for the content
* and return pointer to the begining of the new list
*/
#include "libft.h"
t_list *ft_lstcopy(t_list *lst, void *(*cpy)(void *))
{
t_list *lst_copy;
if (!lst || !cpy)
return (NULL);
lst_copy = NULL;
while (lst)
{
ft_lstpush_back(&lst_copy, ft_lstcreate(cpy(lst->content)));
lst = lst->next;
}
return (ft_lstbegin(lst_copy));
}

31
srcs/ft_lstcreate.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstcreate.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 13:12:50 by simplonco #+# #+# */
/* Updated: 2022/03/24 14:29:04 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* create a new two-way list
*/
#include "libft.h"
t_list *ft_lstcreate(void *content)
{
t_list *lst;
lst = (t_list *)malloc(sizeof(*lst));
if (!lst)
return (NULL);
lst->content = content;
lst->prev = NULL;
lst->next = NULL;
return (lst);
}

24
srcs/ft_lstend.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstend.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:11:41 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:12:17 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* return a pointer to the last element of a two-way list
*/
#include "libft.h"
t_list *ft_lstend(t_list *lst)
{
while (lst->next)
lst = lst->next;
return (lst);
}

30
srcs/ft_lsterase.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lsterase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:31:16 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:40:32 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* erase an element in two-way list
* and rejoin the list
*/
#include "libft.h"
void ft_lsterase(t_list *lst, void (*del)(void *))
{
if (!lst || !del)
return ;
del(lst->content);
lst->prev->next = lst->next;
lst->next->prev = lst->prev;
free(lst);
lst = NULL;
}

25
srcs/ft_lstfind.c Normal file
View File

@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstfind.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/23 22:48:47 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:23:22 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* find an element in two-way list with the comp function
* return a pointer to it
*/
#include "libft.h"
t_list *ft_lstfind(t_list *lst, void *to_find, int (*comp)(void*, void *))
{
while (lst && (!comp(to_find, lst->content)))
lst = lst->next;
return (lst);
}

33
srcs/ft_lstfree.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstfree.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:49:35 by simplonco #+# #+# */
/* Updated: 2022/03/24 16:16:47 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* delete and free an element of a two-way list and all the followings
*/
#include "libft.h"
void ft_lstfree(t_list *lst, void (*del)(void *))
{
t_list *next;
if (lst && lst->prev)
lst->prev->next = NULL;
while (lst != NULL)
{
next = lst->next;
del(lst->content);
free(lst);
lst = next;
}
}

27
srcs/ft_lstinsert.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstinsert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:22:46 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:31:52 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* insert an element in two-way list just after the one in parameter
* and rejoin the list
*/
#include "libft.h"
void ft_lstinsert(t_list *lst, t_list *new)
{
new->next = lst->next;
lst->next = new;
new->next->prev = new;
new->prev = lst;
}

30
srcs/ft_lstlen.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 16:20:25 by simplonco #+# #+# */
/* Updated: 2022/03/24 16:22:24 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* return the len of the two-way list
*/
#include "libft.h"
int ft_lstlen(t_list *lst)
{
int size;
size = 0;
while (lst)
{
size++;
lst = lst->next;
}
return (size);
}

30
srcs/ft_lstloop.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstloop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 14:37:15 by simplonco #+# #+# */
/* Updated: 2022/03/24 14:38:32 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* go forward through all elements of a two-way list
* and apply the function f to each of them
*/
#include "libft.h"
void ft_lstloop(t_list *lst, void (*f)(void *))
{
if (!f)
return ;
while (lst)
{
f(lst->content);
lst = lst->next;
}
}

30
srcs/ft_lstloop_back.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstloop_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:01:37 by simplonco #+# #+# */
/* Updated: 2022/03/24 15:02:02 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* go backward through all elements of a two-way list
* and apply the function f to each of them
*/
#include "libft.h"
void ft_lstloop_back(t_list *lst, void (*f)(void *))
{
if (!f)
return ;
while (lst)
{
f(lst->content);
lst = lst->prev;
}
}

41
srcs/ft_lstpush_back.c Normal file
View File

@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstpush_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 13:37:11 by simplonco #+# #+# */
/* Updated: 2022/03/24 14:28:49 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* add an element to the end of a two-way list, or first if list has no element
* return NULL if element is NULL (eg it returned from ft_lstcreate and failed)
* or else address to the element added
*/
#include "libft.h"
void *ft_lstpush_back(t_list **lst, t_list *new)
{
t_list *tmp;
if (!new)
return (NULL);
if (lst)
{
tmp = *lst;
if (!tmp)
*lst = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
new->prev = tmp;
}
}
return (new);
}

24
srcs/ft_lstpush_front.c Normal file
View File

@@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstpush_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 14:25:25 by simplonco #+# #+# */
/* Updated: 2022/03/24 14:32:14 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* add an element to the begining of a two-way list
*/
#include "libft.h"
void ft_lstpush_front(t_list **alst, t_list *new)
{
new->next = *alst;
(*alst)->prev = new;
*alst = new;
}

31
srcs/ft_lstremove.delete Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstremove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/23 22:44:55 by simplonco #+# #+# */
/* Updated: 2022/03/23 22:46:30 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* remove an element of the list, by moving the content of the next element
* and delete and free the next element
*/
#include "libft.h"
void ft_lstremove(t_list *lst, void (*del)(void *))
{
t_list *next_tmp;
if (!lst || !del)
return ;
next_tmp = lst->next;
del(lst->content);
lst->content = lst->next->content;
lst->next = lst->next->next;
free(next_tmp);
}