add lstremove_next and lstadd_back now return

This commit is contained in:
hugogogo
2022-03-23 21:52:04 +01:00
parent 39b3f2bc4e
commit bc53060626
5 changed files with 47 additions and 13 deletions

View File

@@ -120,7 +120,8 @@ SRCS = ft_memset.c \
ft_sqrt.c \
ft_free_tab.c \
\
ft_arrintchr.c
ft_arrintchr.c \
ft_lstremove_next.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 14:22:33 by simplonco ### ########.fr */
/* Updated: 2022/03/23 21:51:14 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
@@ -65,9 +65,9 @@ typedef struct s_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);
int ft_lstsize(t_list *lst);
t_list *ft_lstlast(t_list *lst);
void ft_lstadd_back(t_list **alst, t_list *n);
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 *));
@@ -114,5 +114,6 @@ 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

View File

@@ -6,13 +6,14 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:11:53 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:36:12 by hulamy ### ########.fr */
/* Updated: 2022/03/23 21:50:42 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
** add an element to the end of a list
** or first if list has no element so far
** add an element to the end of a list, or first if list has no element yet
** return NULL if element is NULL (eg, it returned from ft_lstnew and failed)
** or else address to the element added
*/
/*
@@ -71,10 +72,12 @@
#include "libft.h"
void ft_lstadd_back(t_list **alst, t_list *new)
void *ft_lstadd_back(t_list **alst, t_list *new)
{
t_list *tmp;
if (!new)
return (NULL);
if (alst)
{
tmp = *alst;
@@ -87,4 +90,5 @@ void ft_lstadd_back(t_list **alst, t_list *new)
tmp->next = new;
}
}
return (tmp);
}

View File

@@ -6,7 +6,7 @@
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 14:16:20 by hulamy #+# #+# */
/* Updated: 2019/11/25 14:29:46 by hulamy ### ########.fr */
/* Updated: 2022/03/23 20:24:40 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
@@ -48,12 +48,10 @@ t_list *ft_lstnew(void *content)
{
t_list *lst;
if (!(lst = (t_list *)malloc(sizeof(*lst))))
lst = (t_list *)malloc(sizeof(*lst));
if (!lst)
return (NULL);
if (!content)
lst->content = NULL;
else
lst->content = content;
lst->content = content;
lst->next = NULL;
return (lst);
}

30
srcs/ft_lstremove_next.c Normal file
View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstremove_next.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simplonco <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/23 21:42:05 by simplonco #+# #+# */
/* Updated: 2022/03/23 21:42:55 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
/*
* remove the next element of the list, delete its content, and free it
* then rejoin the list without this element
*/
#include "libft.h"
void ft_lstremove_next(t_list *lst, void (*del)(void *))
{
t_list *next_tmp;
if (!lst || !lst->next || !del)
return ;
next_tmp = lst->next->next;
del(lst->next->content);
free(lst->next);
lst->next = next_tmp;
}