diff options
Diffstat (limited to 'test_mini/libft/src/lst')
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstbsearch.c | 65 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstdelone.c | 27 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstdestroy.c | 29 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstiter.c | 29 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstlast.c | 27 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstlfind.c | 22 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstlsearch.c | 27 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstmap.c | 56 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstnew.c | 29 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstpop_front.c | 29 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstpush_back.c | 30 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstpush_front.c | 26 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstremove_if.c | 38 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstreverse.c | 22 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstreverse_ret.c | 32 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstsize.c | 31 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstsort.c | 46 | ||||
| -rw-r--r-- | test_mini/libft/src/lst/ft_lstsorted_merge.c | 43 |
18 files changed, 608 insertions, 0 deletions
diff --git a/test_mini/libft/src/lst/ft_lstbsearch.c b/test_mini/libft/src/lst/ft_lstbsearch.c new file mode 100644 index 0000000..0c48eb0 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstbsearch.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:12 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_lst.h" + +static t_ftlst *st_lstmiddle(t_ftlst *lst, t_ftlst *last) +{ + t_ftlst *slow; + t_ftlst *fast; + + if (lst == NULL) + return (NULL); + slow = lst; + fast = lst; + while (fast != last) + { + fast = fast->next; + if (fast == last) + break ; + slow = slow->next; + fast = fast->next; + } + return (slow); +} + +static t_ftlst *st_lstbsearch_rec(t_ftlst *lst, t_ftlst *last, + t_ftcompar_func cmp, const void *ref) +{ + int res; + t_ftlst *mid; + + if (lst == NULL) + return (NULL); + mid = st_lstmiddle(lst, last); + if (mid == NULL) + return (NULL); + if (mid->next == NULL) + { + if (cmp(ref, mid->data) == 0) + return (mid); + return (NULL); + } + res = cmp(ref, mid->next->data); + if (res < 0) + return (st_lstbsearch_rec(lst, mid, cmp, ref)); + else if (res > 0) + return (st_lstbsearch_rec(mid->next->next, NULL, cmp, ref)); + return (mid->next); +} + +t_ftlst *ft_lstbsearch(t_ftlst *lst, t_ftcompar_func cmp, + const void *ref) +{ + return (st_lstbsearch_rec(lst, NULL, cmp, ref)); +} diff --git a/test_mini/libft/src/lst/ft_lstdelone.c b/test_mini/libft/src/lst/ft_lstdelone.c new file mode 100644 index 0000000..3dfbbbb --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstdelone.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:02 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Delete list node +** \param del Delete function for node's data +*/ + +void ft_lstdelone(t_ftlst *lst, void (*del)(void *)) +{ + if (lst == NULL) + return ; + if (del != NULL) + (*del)(lst->data); + free(lst); +} diff --git a/test_mini/libft/src/lst/ft_lstdestroy.c b/test_mini/libft/src/lst/ft_lstdestroy.c new file mode 100644 index 0000000..35da2a5 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstdestroy.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:39 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:37 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Destroy a list and set his pointer to NULL +** \param del Delete Function for data of each node +*/ + +void ft_lstdestroy(t_ftlst **lst, void (*del)(void *)) +{ + if (lst == NULL) + return ; + if (*lst == NULL) + return ; + ft_lstdestroy(&((*lst)->next), del); + ft_lstdelone(*lst, del); + *lst = NULL; +} diff --git a/test_mini/libft/src/lst/ft_lstiter.c b/test_mini/libft/src/lst/ft_lstiter.c new file mode 100644 index 0000000..e46b507 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstiter.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstiter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:22 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:39 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Iterate of list +** \param f Funtion applied to data of each node +*/ + +void ft_lstiter(t_ftlst *lst, void (*f)(void *)) +{ + if (f == NULL) + return ; + while (lst != NULL) + { + (*f)(lst->data); + lst = lst->next; + } +} diff --git a/test_mini/libft/src/lst/ft_lstlast.c b/test_mini/libft/src/lst/ft_lstlast.c new file mode 100644 index 0000000..97b853d --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstlast.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:40 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:02:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Last node +** \return List's last node +*/ + +t_ftlst *ft_lstlast(t_ftlst *lst) +{ + if (lst == NULL) + return (NULL); + while (lst->next != NULL) + lst = lst->next; + return (lst); +} diff --git a/test_mini/libft/src/lst/ft_lstlfind.c b/test_mini/libft/src/lst/ft_lstlfind.c new file mode 100644 index 0000000..fd7e688 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstlfind.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlfind.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 18:00:37 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:05 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstlfind(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) +{ + if (lst == NULL) + return (NULL); + if (cmp(ref, lst->data) == 0) + return (lst); + return (ft_lstlfind(lst->next, cmp, ref)); +} diff --git a/test_mini/libft/src/lst/ft_lstlsearch.c b/test_mini/libft/src/lst/ft_lstlsearch.c new file mode 100644 index 0000000..11c528c --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstlsearch.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstlsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/27 16:18:33 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:24:31 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +t_ftlst *ft_lstlsearch(t_ftlst *lst, t_ftcompar_func cmp, const void *ref) +{ + if (lst == NULL) + return (ft_lstnew(ref)); + if (cmp(ref, lst->data) == 0) + return (lst); + if (lst->next == NULL) + { + lst->next = ft_lstnew(ref); + return (lst->next); + } + return (ft_lstlsearch(lst->next, cmp, ref)); +} diff --git a/test_mini/libft/src/lst/ft_lstmap.c b/test_mini/libft/src/lst/ft_lstmap.c new file mode 100644 index 0000000..3182bb0 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstmap.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstmap.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:03:57 by cacharle #+# #+# */ +/* Updated: 2020/02/15 23:11:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Clone a list and map a function to each node data +** \param lst Origin list +** \param f Function applied to each node's data +** \param del Delete function for cleanning up in case of failed allocation +** \return Mapped clone list +*/ + +t_ftlst *ft_lstmap(t_ftlst *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_ftlst *mapped; + t_ftlst *tmp; + + if (lst == NULL || f == NULL) + return (NULL); + mapped = NULL; + while (lst != NULL) + { + if ((tmp = ft_lstnew((*f)(lst->data))) == NULL) + { + ft_lstdestroy(&mapped, del); + return (NULL); + } + ft_lstpush_back(&mapped, tmp); + lst = lst->next; + } + return (mapped); +} + +/* +** Rest in peace, my beautiful recursion. +** +** t_ftlst *tmp; +** +** if (lst == NULL) +** return (NULL); +** if ((tmp = ft_lstnew(lst->data)) == NULL) +** return (NULL); +** tmp->data = (*f)(tmp->data); +** tmp->next = ft_lstmap(lst->next, f); +** return (tmp); +*/ diff --git a/test_mini/libft/src/lst/ft_lstnew.c b/test_mini/libft/src/lst/ft_lstnew.c new file mode 100644 index 0000000..1616b71 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstnew.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:01:16 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:35 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Create a list node +** \param data Pointer to data of node +*/ + +t_ftlst *ft_lstnew(void const *data) +{ + t_ftlst *elem; + + if ((elem = (t_ftlst*)malloc(sizeof(t_ftlst))) == NULL) + return (NULL); + elem->data = (void*)data; + elem->next = NULL; + return (elem); +} diff --git a/test_mini/libft/src/lst/ft_lstpop_front.c b/test_mini/libft/src/lst/ft_lstpop_front.c new file mode 100644 index 0000000..a61350a --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstpop_front.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpop_front_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:12:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Delete head node and replace it with next node +** \param del Delete function for node data +*/ + +void ft_lstpop_front(t_ftlst **lst, void (*del)(void *)) +{ + t_ftlst *tmp; + + if (lst == NULL || *lst == NULL) + return ; + tmp = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = tmp; +} diff --git a/test_mini/libft/src/lst/ft_lstpush_back.c b/test_mini/libft/src/lst/ft_lstpush_back.c new file mode 100644 index 0000000..1dca078 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstpush_back.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_back.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:03 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:26 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Push new node to the list end +** \param new Pushed node +*/ + +void ft_lstpush_back(t_ftlst **alst, t_ftlst *new) +{ + if (alst == NULL) + return ; + if (*alst == NULL) + { + *alst = new; + return ; + } + ft_lstlast(*alst)->next = new; +} diff --git a/test_mini/libft/src/lst/ft_lstpush_front.c b/test_mini/libft/src/lst/ft_lstpush_front.c new file mode 100644 index 0000000..85df649 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstpush_front.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstpush_front.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:02:25 by cacharle #+# #+# */ +/* Updated: 2019/10/18 12:16:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Push node to list front +** \param new Pushed node +*/ + +void ft_lstpush_front(t_ftlst **alst, t_ftlst *new) +{ + if (alst == NULL || new == NULL) + return ; + new->next = *alst; + *alst = new; +} diff --git a/test_mini/libft/src/lst/ft_lstremove_if.c b/test_mini/libft/src/lst/ft_lstremove_if.c new file mode 100644 index 0000000..4070355 --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstremove_if.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstremove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ +/* Updated: 2020/02/28 12:20:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Remove node on some condition +** \param cmp Comparison function, return 0 if equal +** \param ref Reference data passed has the first arg of `cmp` +** \param del Delete function to free removed node data +*/ + +void ft_lstremove_if(t_ftlst **lst, t_ftcompar_func cmp, + const void *ref, t_ftdel_func del) +{ + t_ftlst *saved_next; + + if (lst == NULL || *lst == NULL) + return ; + if (cmp(ref, (*lst)->data) == 0) + { + saved_next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = saved_next; + ft_lstremove_if(lst, cmp, ref, del); + return ; + } + ft_lstremove_if(&(*lst)->next, cmp, ref, del); +} diff --git a/test_mini/libft/src/lst/ft_lstreverse.c b/test_mini/libft/src/lst/ft_lstreverse.c new file mode 100644 index 0000000..7c2778d --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstreverse.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstreverse.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 12:49:14 by cacharle #+# #+# */ +/* Updated: 2020/02/15 22:53:23 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Reverse a list +*/ + +void ft_lstreverse(t_ftlst **lst) +{ + *lst = ft_lstreverse_ret(*lst); +} diff --git a/test_mini/libft/src/lst/ft_lstreverse_ret.c b/test_mini/libft/src/lst/ft_lstreverse_ret.c new file mode 100644 index 0000000..36c0c5c --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstreverse_ret.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstreverse_ret.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/15 12:51:15 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:20:21 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Reverse list +** \return Pointer to reversed list +*/ + +t_ftlst *ft_lstreverse_ret(t_ftlst *lst) +{ + t_ftlst *tmp; + + if (lst == NULL) + return (NULL); + if (lst->next == NULL) + return (lst); + tmp = ft_lstreverse_ret(lst->next); + lst->next->next = lst; + lst->next = NULL; + return (tmp); +} diff --git a/test_mini/libft/src/lst/ft_lstsize.c b/test_mini/libft/src/lst/ft_lstsize.c new file mode 100644 index 0000000..3c6956b --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstsize.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsize.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/10/09 09:04:28 by cacharle #+# #+# */ +/* Updated: 2019/11/20 04:01:44 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief List size +** \return Number of node in list +*/ + +int ft_lstsize(t_ftlst *lst) +{ + int counter; + + counter = 0; + while (lst != NULL) + { + counter++; + lst = lst->next; + } + return (counter); +} diff --git a/test_mini/libft/src/lst/ft_lstsort.c b/test_mini/libft/src/lst/ft_lstsort.c new file mode 100644 index 0000000..9945a0f --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstsort.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsort.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:53:55 by cacharle #+# #+# */ +/* Updated: 2020/02/16 02:18:05 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Sort list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \note Use merge sort algorithm +*/ + +void ft_lstsort(t_ftlst **begin_list, t_ftcompar_func cmp) +{ + t_ftlst *fast; + t_ftlst *slow; + t_ftlst *middle; + + if (begin_list == NULL || *begin_list == NULL + || (*begin_list)->next == NULL) + return ; + fast = (*begin_list)->next; + slow = *begin_list; + while (fast != NULL) + { + fast = fast->next; + if (fast != NULL) + { + fast = fast->next; + slow = slow->next; + } + } + middle = slow->next; + slow->next = NULL; + ft_lstsort(begin_list, cmp); + ft_lstsort(&middle, cmp); + *begin_list = ft_lstsorted_merge(*begin_list, middle, cmp); +} diff --git a/test_mini/libft/src/lst/ft_lstsorted_merge.c b/test_mini/libft/src/lst/ft_lstsorted_merge.c new file mode 100644 index 0000000..995785f --- /dev/null +++ b/test_mini/libft/src/lst/ft_lstsorted_merge.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstsorted_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/10 01:58:52 by cacharle #+# #+# */ +/* Updated: 2020/02/16 02:18:11 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_lst.h" + +/* +** \brief Merge sorted lists, the new list is also sorted +** \param l1 First list +** \param l2 Second list +** \param cmp Comparison function, <0 if less, 0 if equal, >0 if greater +** \return Pointer to first node of merged list +*/ + +t_ftlst *ft_lstsorted_merge(t_ftlst *l1, t_ftlst *l2, t_ftcompar_func cmp) +{ + t_ftlst *merged; + + merged = NULL; + if (l1 == NULL) + return (l2); + if (l2 == NULL) + return (l1); + if (cmp(l1->data, l2->data) < 0) + { + merged = l1; + merged->next = ft_lstsorted_merge(l1->next, l2, cmp); + } + else + { + merged = l2; + merged->next = ft_lstsorted_merge(l1, l2->next, cmp); + } + return (merged); +} |
