Skip to content

[LIB] Add new linked list functions to libft #341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion libraries/libft/build/libft.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/16 13:33:38 by ldulling #+# #+# #
# Updated: 2024/06/28 19:31:23 by ldulling ### ########.fr #
# Updated: 2025/05/28 19:26:09 by ldulling ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -79,23 +79,27 @@ SRC += $(addprefix $(DIR), \
ft_lstlast_d.c \
ft_lstnew_back_d.c \
ft_lstnew_d.c \
ft_lstsize_d.c \
)

# Singly-linked:
DIR := lists/singly_linked/
SRC += $(addprefix $(DIR), \
ft_lstadd_back.c \
ft_lstadd_back_tail.c \
ft_lstadd_front.c \
ft_lstclear.c \
ft_lstdelone.c \
ft_lstdrop_node.c \
ft_lstdup.c \
ft_lstinsert_after.c \
ft_lstinsert_before.c \
ft_lstiter.c \
ft_lstlast.c \
ft_lstmap.c \
ft_lstnew.c \
ft_lstnew_back.c \
ft_lstnew_back_tail.c \
ft_lstnew_front.c \
ft_lstpop_front.c \
ft_lstpop_front_content.c \
Expand Down
6 changes: 5 additions & 1 deletion libraries/libft/inc/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */
/* Updated: 2024/05/21 14:58:24 by ldulling ### ########.fr */
/* Updated: 2025/05/28 19:26:17 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -60,22 +60,26 @@ void ft_lstiter_d(t_list_d *lst, void (*f)(void *));
t_list_d *ft_lstlast_d(t_list_d *lst);
bool ft_lstnew_back_d(t_list_d **lst, void *content);
t_list_d *ft_lstnew_d(void *content);
int ft_lstsize_d(t_list_d *lst);

\
/* Lists singly-linked */

void ft_lstadd_back(t_list **lst, t_list *new);
void ft_lstadd_back_tail(t_list **lst, t_list **tail, t_list *new);
void ft_lstadd_front(t_list **lst, t_list *new);
void ft_lstclear(t_list **lst, void (*del)(void *));
void ft_lstdelone(t_list *lst, void (*del)(void *));
void ft_lstdrop_node(t_list **lst, t_list **node, void (*del)(void *));
t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *));
void ft_lstinsert_after(t_list **lst, t_list *new);
void ft_lstinsert_before(t_list **lst, t_list *cur, t_list *new);
void ft_lstiter(t_list *lst, void (*f)(void *));
t_list *ft_lstlast(t_list *lst);
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
t_list *ft_lstnew(void *content);
bool ft_lstnew_back(t_list **lst, void *content);
bool ft_lstnew_back_tail(t_list **lst, t_list **tail, void *content);
bool ft_lstnew_front(t_list **lst, void *content);
t_list *ft_lstpop_front(t_list **lst);
void *ft_lstpop_front_content(t_list **lst);
Expand Down
28 changes: 28 additions & 0 deletions libraries/libft/src/lists/doubly_linked/ft_lstsize_d.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsize_d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:08:06 by ldulling #+# #+# */
/* Updated: 2024/06/28 19:30:32 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int ft_lstsize_d(t_list_d *lst)
{
int n;
t_list_d *cur;

n = 0;
cur = lst;
while (cur != NULL)
{
n++;
cur = cur->next;
}
return (n);
}
43 changes: 43 additions & 0 deletions libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back_tail.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 00:35:15 by ldulling #+# #+# */
/* Updated: 2025/05/28 21:34:35 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstadd_back_tail function adds a node to the end of the singly linked
* list.
* It uses the tail pointer of the list to avoid traversing the whole list.
* The tail pointer is updated to point to the new node.
*
* @param lst The address of the pointer to the first node of the list.
* @param tail The address of the pointer to the last node of the list.
* If tail (not *tail) is NULL, it is ignored.
* @param new The new node to be added to the list.
* If new is NULL, the function does nothing.
*
* @return No return value.
*
*/
void ft_lstadd_back_tail(t_list **lst, t_list **tail, t_list *new)
{
if (lst == NULL || tail == NULL || new == NULL)
{
ft_lstadd_back(lst, new);
return ;
}
if (*lst == NULL || *tail == NULL)
ft_lstadd_back(lst, new);
else
(*tail)->next = new;
*tail = new;
return ;
}
52 changes: 52 additions & 0 deletions libraries/libft/src/lists/singly_linked/ft_lstdup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 00:35:12 by ldulling #+# #+# */
/* Updated: 2025/05/28 19:27:00 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstdup function duplicates a singly linked list by creating new nodes
* with the same content as the original list. The content of the new nodes is
* created by calling the provided 'dup' function on the content of the original
* nodes.
*
* @param lst The original list to be duplicated.
* @param dup The function to duplicate the content of the nodes.
* @param del The function to delete the content of the nodes in case of
* failure.
*
* @return Returns a pointer to the first node of the new list, or NULL if
* the list could not be duplicated.
*
*/
t_list *ft_lstdup(t_list *lst, void *(*dup)(void *), void (*del)(void *))
{
t_list *cur;
t_list *new_lst;
t_list *new_lst_tail;
void *new_content;

if (lst == NULL || dup == NULL || del == NULL)
return (NULL);
new_lst = NULL;
new_lst_tail = NULL;
cur = lst;
while (cur != NULL)
{
new_content = (*dup)(cur->content);
if (new_content == NULL)
return (ft_lstclear(&new_lst, del), NULL);
if (!ft_lstnew_back_tail(&new_lst, &new_lst_tail, new_content))
return (del(new_content), ft_lstclear(&new_lst, del), NULL);
cur = cur->next;
}
return (new_lst);
}
16 changes: 2 additions & 14 deletions libraries/libft/src/lists/singly_linked/ft_lstmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:04:35 by ldulling #+# #+# */
/* Updated: 2023/12/23 11:43:14 by ldulling ### ########.fr */
/* Updated: 2025/05/28 19:27:05 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node);

t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
{
t_list *cur;
Expand All @@ -37,18 +35,8 @@ t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *))
ft_lstclear(&new_lst, del);
return (NULL);
}
lstadd_back_eff(&new_lst, &new_lst_tail, &new_node);
ft_lstadd_back_tail(&new_lst, &new_lst_tail, new_node);
cur = cur->next;
}
return (new_lst);
}

static void lstadd_back_eff(t_list **head, t_list **tail, t_list **new_node)
{
if (*head == NULL)
*head = *new_node;
else
(*tail)->next = *new_node;
*tail = *new_node;
return ;
}
6 changes: 4 additions & 2 deletions libraries/libft/src/lists/singly_linked/ft_lstnew_back.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 20:30:19 by ldulling #+# #+# */
/* Updated: 2024/01/17 11:42:57 by ldulling ### ########.fr */
/* Updated: 2025/05/28 20:14:28 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,13 +20,15 @@
* @param content The content to be added to the new node.
*
* @return Returns true if the new node was successfully added, false
* if malloc failed.
* if malloc failed or lst (not *lst) is NULL.
*
*/
bool ft_lstnew_back(t_list **lst, void *content)
{
t_list *new_node;

if (lst == NULL)
return (false);
new_node = ft_lstnew(content);
if (new_node == NULL)
return (false);
Expand Down
41 changes: 41 additions & 0 deletions libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_back_tail.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 19:11:17 by ldulling #+# #+# */
/* Updated: 2025/05/28 20:24:43 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstnew_back_tail function creates a new node with the provided content
* and adds it to the end of the singly linked list.
* It uses the tail pointer of the list to avoid traversing the whole list.
* The tail pointer is updated to point to the new node.
*
* @param lst The address of the list to add the new node to.
* @param tail The address of the pointer to the last node of the list.
* If tail (not *tail) is NULL, it is ignored.
* @param content The content to be added to the new node.
*
* @return Returns true if the new node was successfully added, false
* if malloc failed or lst (not *lst) is NULL.
*
*/
bool ft_lstnew_back_tail(t_list **lst, t_list **tail, void *content)
{
t_list *new_node;

if (lst == NULL || tail == NULL)
return (ft_lstnew_back(lst, content));
new_node = ft_lstnew(content);
if (new_node == NULL)
return (false);
ft_lstadd_back_tail(lst, tail, new_node);
return (true);
}
6 changes: 4 additions & 2 deletions libraries/libft/src/lists/singly_linked/ft_lstnew_front.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ldulling <ldulling@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/25 16:09:14 by ldulling #+# #+# */
/* Updated: 2024/01/25 16:09:17 by ldulling ### ########.fr */
/* Updated: 2025/05/28 20:14:18 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,13 +20,15 @@
* @param content The content to be added to the new node.
*
* @return Returns true if the new node was successfully added, false if
* malloc failed.
* malloc failed or lst (not *lst) is NULL.
*
*/
bool ft_lstnew_front(t_list **lst, void *content)
{
t_list *new_node;

if (lst == NULL)
return (false);
new_node = ft_lstnew(content);
if (new_node == NULL)
return (false);
Expand Down