Skip to content

Commit 703acd4

Browse files
committed
bonus part
1 parent b40067d commit 703acd4

10 files changed

+118
-14
lines changed

Makefile

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: phelebra <[email protected]> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2023/01/13 09:54:20 by phelebra #+# #+# #
9-
# Updated: 2023/01/21 18:41:53 by phelebra ### ########.fr #
9+
# Updated: 2023/01/21 22:50:56 by phelebra ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -25,10 +25,8 @@ SRCS := ft_isascii.c ft_isprint.c ft_isalpha.c ft_isdigit.c ft_isalnum.c \
2525
ft_putendl_fd.c ft_putnbr_fd.c ft_strmapi.c ft_itoa.c \
2626
ft_split.c
2727

28-
BONUS_S := ft_lstnew.c \
29-
#ft_lstadd_front.c ft_lstsize.c \
30-
ft_lstlast.c ft_lstadd_back.c ft_lstdelone.c \
31-
ft_lstclear.c ft_lstiter.c ft_lstmap.c
28+
BONUS_S := ft_lstnew.c ft_lstsize.c ft_lstlast.c ft_lstmap.c ft_lstiter.c \
29+
ft_lstadd_back.c ft_lstdelone.c ft_lstclear.c ft_lstadd_front.c \
3230

3331
OBJS := $(SRCS:.c=.o)
3432
BONUS_O := $(BONUS_S:.c=.o)

ft_lstadd_back.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:21 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:03 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:26:44 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
void ft_lstadd_back(t_list **lst, t_list *new)
16+
{
17+
t_list *last;
18+
19+
if (!lst || !*lst)
20+
{
21+
*lst = new;
22+
return ;
23+
}
24+
last = ft_lstlast(*lst);
25+
last->next = new;
26+
}

ft_lstadd_front.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:41:52 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:06 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:07:36 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
void ft_lstadd_front(t_list **lst, t_list *new)
16+
{
17+
new->next = *lst;
18+
*lst = new;
19+
}

ft_lstclear.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,23 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:39 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:09 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:42:56 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
void ft_lstclear(t_list **lst, void (*del)(void*))
16+
{
17+
t_list *clear;
18+
19+
if (!*lst)
20+
return ;
21+
while (*lst)
22+
{
23+
clear = (*lst)->next;
24+
ft_lstdelone(*lst, del);
25+
*lst = clear;
26+
}
27+
*lst = NULL;
28+
}

ft_lstdelone.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,16 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:29 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:13 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:33:28 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
void ft_lstdelone(t_list *lst, void (*del)(void*))
16+
{
17+
if (!lst)
18+
return ;
19+
del(lst->content);
20+
free(lst);
21+
}

ft_lstiter.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:52 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:16 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:52:39 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
void ft_lstiter(t_list *lst, void (*f)(void *))
16+
{
17+
if (!lst)
18+
return ;
19+
while (lst)
20+
{
21+
f(lst->content);
22+
lst = lst->next;
23+
}
24+
}

ft_lstlast.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,17 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:10 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:19 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:19:04 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
t_list *ft_lstlast(t_list *lst)
16+
{
17+
if (lst == NULL)
18+
return (NULL);
19+
while (lst->next != NULL)
20+
lst = lst->next;
21+
return (lst);
22+
}

ft_lstmap.c

+24-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:43:07 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:22 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 23:14:08 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
16+
{
17+
t_list *new;
18+
t_list *tmp;
19+
20+
if (!lst || !del || !f)
21+
return (NULL);
22+
new = 0;
23+
while (lst)
24+
{
25+
tmp = ft_lstnew((*f)(lst->content));
26+
if (!tmp)
27+
{
28+
ft_lstclear(&new, del);
29+
return (NULL);
30+
}
31+
else
32+
ft_lstadd_back(&new, tmp);
33+
lst = lst->next;
34+
}
35+
return (new);
36+
}

ft_lstsize.c

+14-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,21 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:42:01 by phelebra #+# #+# */
9-
/* Updated: 2023/01/13 10:44:32 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 22:45:58 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
14+
15+
int ft_lstsize(t_list *lst)
16+
{
17+
int i;
18+
19+
i = 0;
20+
while (lst)
21+
{
22+
i++;
23+
lst = lst->next;
24+
}
25+
return (i);
26+
}

libft.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/13 10:17:52 by phelebra #+# #+# */
9-
/* Updated: 2023/01/21 18:45:55 by phelebra ### ########.fr */
9+
/* Updated: 2023/01/21 23:06:11 by phelebra ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -56,5 +56,13 @@ char *ft_itoa(int n);
5656
char *ft_strtrim(char const *s1, char const *set);
5757
char **ft_split(char const *s, char c);
5858
t_list *ft_lstnew(void *content);
59+
void ft_lstadd_front(t_list **lst, t_list *new);
60+
int ft_lstsize(t_list *lst);
61+
t_list *ft_lstlast(t_list *lst);
62+
void ft_lstadd_back(t_list **lst, t_list *new);
63+
void ft_lstdelone(t_list *lst, void (*del)(void*));
64+
void ft_lstclear(t_list **lst, void (*del)(void*));
65+
void ft_lstiter(t_list *lst, void (*f)(void *));
66+
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
5967

6068
#endif

0 commit comments

Comments
 (0)