Adding the libft

This commit is contained in:
Raphael 2024-10-31 17:24:36 +01:00
parent 1b98b537b7
commit c947e43e51
62 changed files with 2098 additions and 19 deletions

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:39:56 by rparodi #+# #+# */
/* Updated: 2023/11/12 17:11:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_back(t_list **lst, t_list *new)
{
t_list *tempo;
if (*lst == NULL)
{
*lst = new;
return ;
}
tempo = *lst;
while (tempo->next != NULL)
tempo = tempo->next;
tempo->next = new;
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:40:16 by rparodi #+# #+# */
/* Updated: 2023/11/12 17:14:33 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstadd_front(t_list **lst, t_list *new)
{
if (*lst == NULL)
{
*lst = new;
return ;
}
new->next = *lst;
*lst = new;
}

25
libft/list/ft_lstclear.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstclear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:40:37 by rparodi #+# #+# */
/* Updated: 2023/11/12 19:34:19 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstclear(t_list **lst, void (*del)(void *))
{
t_list *tempo;
while (*lst)
{
tempo = (*lst)->next;
ft_lstdelone(*lst, del);
(*lst) = tempo;
}
}

19
libft/list/ft_lstdelone.c Normal file
View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdelone.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:42:11 by rparodi #+# #+# */
/* Updated: 2023/11/12 18:10:40 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstdelone(t_list *lst, void (*del)(void *))
{
del(lst->content);
free(lst);
}

22
libft/list/ft_lstiter.c Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstiter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:42:24 by rparodi #+# #+# */
/* Updated: 2023/11/12 18:07:02 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_lstiter(t_list *lst, void (*f)(void *))
{
while (lst != NULL)
{
f(lst->content);
lst = lst->next;
}
}

22
libft/list/ft_lstlast.c Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstlast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:42:54 by rparodi #+# #+# */
/* Updated: 2023/11/12 17:25:48 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstlast(t_list *lst)
{
if (lst == NULL)
return (lst);
while (lst->next != NULL)
lst = lst->next;
return (lst);
}

37
libft/list/ft_lstmap.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:43:28 by rparodi #+# #+# */
/* Updated: 2023/11/12 19:28:02 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *tempo;
t_list *tempo_content;
tempo_content = ft_lstnew(f(lst->content));
if (!tempo_content)
return (NULL);
tempo = tempo_content;
lst = lst->next;
while (lst)
{
tempo_content = ft_lstnew(f(lst->content));
if (!tempo_content)
{
ft_lstclear(&tempo, del);
return (NULL);
}
ft_lstadd_back(&tempo, tempo_content);
lst = lst->next;
}
return (tempo);
}

26
libft/list/ft_lstnew.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:44:02 by rparodi #+# #+# */
/* Updated: 2023/11/12 14:25:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
t_list *ft_lstnew(void *content)
{
t_list *to_return;
to_return = NULL;
to_return = (t_list *)malloc(sizeof(t_list));
if (!to_return)
return (NULL);
to_return->content = content;
to_return->next = NULL;
return (to_return);
}

26
libft/list/ft_lstsize.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/12 11:44:24 by rparodi #+# #+# */
/* Updated: 2023/11/13 12:22:02 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_lstsize(t_list *lst)
{
int count;
count = 0;
while (lst != NULL)
{
lst = lst->next;
count++;
}
return (count);
}