Skip to content
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

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions libraries/libft/build/libft.mk
Original file line number Diff line number Diff line change
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_eff.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_eff.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 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:17:46 by ldulling #+# #+# */
/* Updated: 2024/05/21 14:58:24 by ldulling ### ########.fr */
/* Updated: 2024/06/28 19:31:02 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_eff(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_eff(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 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
36 changes: 36 additions & 0 deletions libraries/libft/src/lists/singly_linked/ft_lstadd_back_eff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstadd_back_eff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 00:35:15 by ldulling #+# #+# */
/* Updated: 2024/06/07 19:10:00 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstadd_back_eff 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.
* @param new The new node to be added to the list.
*
* @return This function does not return a value.
*
*/
void ft_lstadd_back_eff(t_list **lst, t_list **tail, t_list *new)
{
if (*lst == NULL)
*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 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/11 00:35:12 by ldulling #+# #+# */
/* Updated: 2024/06/07 19:09:20 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_eff(&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 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/24 16:04:35 by ldulling #+# #+# */
/* Updated: 2023/12/23 11:43:14 by ldulling ### ########.fr */
/* Updated: 2024/02/11 00:40:18 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_eff(&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 ;
}
38 changes: 38 additions & 0 deletions libraries/libft/src/lists/singly_linked/ft_lstnew_back_eff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstnew_back_eff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldulling <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 19:11:17 by ldulling #+# #+# */
/* Updated: 2024/06/07 19:11:21 by ldulling ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

/**
* The ft_lstnew_back_eff 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.
* @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.
*
*/
bool ft_lstnew_back_eff(t_list **lst, t_list **tail, void *content)
{
t_list *new_node;

new_node = ft_lstnew(content);
if (new_node == NULL)
return (false);
ft_lstadd_back_eff(lst, tail, new_node);
return (true);
}