Skip to content

Commit cc5fb88

Browse files
committed
feat: Add ft_lstadd_back_eff() and ft_lstnew_back_eff() to libft
1 parent 340cf49 commit cc5fb88

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

libraries/libft/build/libft.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ SRC += $(addprefix $(DIR), \
8585
DIR := lists/singly_linked/
8686
SRC += $(addprefix $(DIR), \
8787
ft_lstadd_back.c \
88+
ft_lstadd_back_eff.c \
8889
ft_lstadd_front.c \
8990
ft_lstclear.c \
9091
ft_lstdelone.c \
@@ -96,6 +97,7 @@ SRC += $(addprefix $(DIR), \
9697
ft_lstmap.c \
9798
ft_lstnew.c \
9899
ft_lstnew_back.c \
100+
ft_lstnew_back_eff.c \
99101
ft_lstnew_front.c \
100102
ft_lstpop_front.c \
101103
ft_lstpop_front_content.c \

libraries/libft/inc/libft.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */
9-
/* Updated: 2024/05/21 14:58:24 by ldulling ### ########.fr */
9+
/* Updated: 2024/06/07 18:28:17 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -65,6 +65,7 @@ t_list_d *ft_lstnew_d(void *content);
6565
/* Lists singly-linked */
6666

6767
void ft_lstadd_back(t_list **lst, t_list *new);
68+
void ft_lstadd_back_eff(t_list **lst, t_list **tail, t_list *new);
6869
void ft_lstadd_front(t_list **lst, t_list *new);
6970
void ft_lstclear(t_list **lst, void (*del)(void *));
7071
void ft_lstdelone(t_list *lst, void (*del)(void *));
@@ -76,6 +77,7 @@ t_list *ft_lstlast(t_list *lst);
7677
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
7778
t_list *ft_lstnew(void *content);
7879
bool ft_lstnew_back(t_list **lst, void *content);
80+
bool ft_lstnew_back_eff(t_list **lst, t_list **tail, void *content);
7981
bool ft_lstnew_front(t_list **lst, void *content);
8082
t_list *ft_lstpop_front(t_list **lst);
8183
void *ft_lstpop_front_content(t_list **lst);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_lstadd_back_eff.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/02/11 00:35:15 by ldulling #+# #+# */
9+
/* Updated: 2024/06/07 19:10:00 by ldulling ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
/**
16+
* The ft_lstadd_back_eff function adds a node to the end of the singly linked
17+
* list.
18+
* It uses the tail pointer of the list to avoid traversing the whole list.
19+
* The tail pointer is updated to point to the new node.
20+
*
21+
* @param lst The address of the pointer to the first node of the list.
22+
* @param tail The address of the pointer to the last node of the list.
23+
* @param new The new node to be added to the list.
24+
*
25+
* @return This function does not return a value.
26+
*
27+
*/
28+
void ft_lstadd_back_eff(t_list **lst, t_list **tail, t_list *new)
29+
{
30+
if (*lst == NULL)
31+
*lst = new;
32+
else
33+
(*tail)->next = new;
34+
*tail = new;
35+
return ;
36+
}

libraries/libft/src/lists/singly_linked/ft_lstmap.c

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/09/24 16:04:35 by ldulling #+# #+# */
9-
/* Updated: 2023/12/23 11:43:14 by ldulling ### ########.fr */
9+
/* Updated: 2024/02/11 00:40:18 by ldulling ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "libft.h"
1414

15-
static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node);
16-
1715
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
1816
{
1917
t_list *cur;
@@ -37,18 +35,8 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
3735
ft_lstclear(&new_lst, del);
3836
return (NULL);
3937
}
40-
lstadd_back_eff(&new_lst, &new_lst_tail, &new_node);
38+
ft_lstadd_back_eff(&new_lst, &new_lst_tail, new_node);
4139
cur = cur->next;
4240
}
4341
return (new_lst);
4442
}
45-
46-
static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node)
47-
{
48-
if (*head == NULL)
49-
*head = *new_node;
50-
else
51-
(*tail)->next = *new_node;
52-
*tail = *new_node;
53-
return ;
54-
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* ft_lstnew_back_eff.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/06/07 19:11:17 by ldulling #+# #+# */
9+
/* Updated: 2024/06/07 19:11:21 by ldulling ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "libft.h"
14+
15+
/**
16+
* The ft_lstnew_back_eff function creates a new node with the provided content
17+
* and adds it to the end of the singly linked list.
18+
* It uses the tail pointer of the list to avoid traversing the whole list.
19+
* The tail pointer is updated to point to the new node.
20+
*
21+
* @param lst The address of the list to add the new node to.
22+
* @param tail The address of the pointer to the last node of the list.
23+
* @param content The content to be added to the new node.
24+
*
25+
* @return Returns true if the new node was successfully added, false
26+
* if malloc failed.
27+
*
28+
*/
29+
bool ft_lstnew_back_eff(t_list **lst, t_list **tail, void *content)
30+
{
31+
t_list *new_node;
32+
33+
new_node = ft_lstnew(content);
34+
if (new_node == NULL)
35+
return (false);
36+
ft_lstadd_back_eff(lst, tail, new_node);
37+
return (true);
38+
}

0 commit comments

Comments
 (0)