Merge branch 'master' of bitbucket.org:hugogogo/libft

This commit is contained in:
hugogogo
2022-05-14 15:27:43 +02:00
34 changed files with 542 additions and 62 deletions

31
srcs/ft_arrint.c Normal file
View File

@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arrint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/21 19:30:25 by simplonco #+# #+# */
/* Updated: 2022/04/21 19:30:35 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* search through an arr of int if it contains the int comp
* return 0 if found, 1 otherwise
*/
#include "libft.h"
int ft_arrint(int * intarr, int comp, size_t size)
{
size_t i;
if (!intarr)
return 1;
i = -1;
while (++i < size)
if (intarr[i] == comp)
return 0;
return 1;
}

View File

@@ -1,90 +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;
}
}
}

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,18 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar.c :+: :+: :+: */
/* ft_lstbegin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:00 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:01 by hulamy ### ########.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"
void ft_putchar(char c)
t_list *ft_lstbegin(t_list *lst)
{
write(1, &c, 1);
while (lst->prev)
lst = lst->prev;
return (lst);
}

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;
}
}

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);
}

View File

@@ -1,97 +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 *))
{
if (lst->content && del)
del(lst->content);
free(lst);
lst = NULL;
}

View File

@@ -1,22 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putstr.c :+: :+: :+: */
/* ft_lstend.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:15:19 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:15:19 by hulamy ### ########.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"
void ft_putstr(char const *s)
t_list *ft_lstend(t_list *lst)
{
int i;
i = 0;
while (s && s[i])
ft_putchar(s[i++]);
while (lst->next)
lst = lst->next;
return (lst);
}

32
srcs/ft_lsterase.c Normal file
View File

@@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lsterase.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/24 15:31:16 by simplonco #+# #+# */
/* Updated: 2022/03/24 20:54:20 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);
if (lst->prev)
lst->prev->next = lst->next;
if (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;
}

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,19 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl.c :+: :+: :+: */
/* ft_lstlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:32 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:33 by hulamy ### ########.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"
void ft_putendl(char const *s)
int ft_lstlen(t_list *lst)
{
ft_putstr(s);
ft_putchar('\n');
int size;
size = 0;
while (lst)
{
size++;
lst = lst->next;
}
return (size);
}

View File

@@ -1,29 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrendl_fd.c :+: :+: :+: */
/* ft_lstloop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/19 10:37:58 by hulamy #+# #+# */
/* Updated: 2019/02/19 10:42:48 by hulamy ### ########.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_putnbrendl_fd(int n, int fd)
void ft_lstloop(t_list *lst, void (*f)(void *))
{
long l;
l = n;
if (l < 0)
if (!f)
return ;
while (lst)
{
ft_putchar_fd('-', fd);
l *= -1;
f(lst->content);
lst = lst->next;
}
if (l >= 10)
ft_putnbr_fd(l / 10, fd);
ft_putchar_fd((l % 10) + '0', fd);
ft_putchar_fd('\n', fd);
}

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;
}
}

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);
}

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);
}

View File

@@ -1,23 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putendl_fd.c :+: :+: :+: */
/* ft_lstpush_front.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 13:59:47 by hulamy #+# #+# */
/* Updated: 2019/11/25 13:59:48 by hulamy ### ########.fr */
/* Created: 2022/03/24 14:25:25 by simplonco #+# #+# */
/* Updated: 2022/03/24 14:32:14 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
** write the string s on the given file descriptor fd, followed by a newline
*/
* add an element to the begining of a two-way list
*/
#include "libft.h"
void ft_putendl_fd(char *s, int fd)
void ft_lstpush_front(t_list **alst, t_list *new)
{
ft_putstr_fd(s, fd);
ft_putchar_fd('\n', fd);
new->next = *alst;
(*alst)->prev = new;
*alst = new;
}

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);
}

View File

@@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:14:57 by hulamy #+# #+# */
/* Updated: 2018/11/14 21:14:58 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbr(int n)
{
ft_putnbr_fd(n, 1);
}

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 15:17:00 by hulamy #+# #+# */
/* Updated: 2018/11/16 15:23:43 by hulamy ### ########.fr */
/* Updated: 2022/03/24 17:01:57 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,13 +47,13 @@ void ft_putnbrbase(int nbr, char *base)
{
if (n < 0)
{
ft_putchar('-');
ft_putchar_fd('-', 1);
n = -n;
}
while (base[i])
i++;
if (n >= i)
ft_putnbrbase(n / i, base);
ft_putchar(base[n % i]);
ft_putchar_fd(base[n % i], 1);
}
}

View File

@@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbrendl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/19 10:38:07 by hulamy #+# #+# */
/* Updated: 2019/02/19 10:42:46 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_putnbrendl(int n)
{
ft_putnbrendl_fd(n, 1);
}