-
Notifications
You must be signed in to change notification settings - Fork 2
[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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
96b3e39
feat: Add `ft_lstadd_back_eff()` and `ft_lstnew_back_eff()` to libft
itislu f51883a
feat: Add `ft_lstdup()` to libft
itislu a9a7bb8
feat: Add `ft_lstsize_d()` to libft
itislu db85524
style: Rename `*_eff` functions to `*_tail`
itislu 3b07bbd
fix: Fix memory leaks if `ft_lst*` function args are `NULL`
itislu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
libraries/libft/src/lists/singly_linked/ft_lstadd_back_tail.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
libraries/libft/src/lists/singly_linked/ft_lstnew_back_tail.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.