From 5bab06313e71e9827baa426a02bbaf2a00b4e6a0 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 24 Jul 2019 08:02:55 +0200 Subject: c12 passed, c13 start --- c12/ex17/ft_list.h | 4 ++-- c12/ex17/ft_sorted_list_merge.c | 47 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) (limited to 'c12/ex17') diff --git a/c12/ex17/ft_list.h b/c12/ex17/ft_list.h index ef3a012..bb1af2d 100644 --- a/c12/ex17/ft_list.h +++ b/c12/ex17/ft_list.h @@ -6,12 +6,12 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/07/09 14:33:17 by cacharle #+# #+# */ -/* Updated: 2019/07/19 08:25:54 by cacharle ### ########.fr */ +/* Updated: 2019/07/23 15:23:07 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef FT_LIST_H -#define FT_LIST_H +# define FT_LIST_H typedef struct s_list { diff --git a/c12/ex17/ft_sorted_list_merge.c b/c12/ex17/ft_sorted_list_merge.c index e69de29..fd19f36 100644 --- a/c12/ex17/ft_sorted_list_merge.c +++ b/c12/ex17/ft_sorted_list_merge.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_sorted_list_merge.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/07/22 08:13:44 by cacharle #+# #+# */ +/* Updated: 2019/07/23 16:09:19 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_list.h" + +static void sorted_list_insert(t_list **begin_list, t_list *elem, int (*cmp)()) +{ + t_list *cursor; + + cursor = *begin_list; + if (cursor == NULL) + { + *begin_list = elem; + return ; + } + if ((*cmp)(elem->data, cursor->data) < 0) + { + elem->next = cursor; + *begin_list = elem; + return ; + } + while (cursor->next && (*cmp)(elem->data, cursor->next->data) > 0) + cursor = cursor->next; + elem->next = cursor->next; + cursor->next = elem; +} + +void ft_sorted_list_merge(t_list **begin_list1, t_list *begin_list2, + int (*cmp)()) +{ + while (begin_list2) + { + printf("%d\n", *(int*)begin_list2->data); + begin_list2 = begin_list2->next; + sorted_list_insert(begin_list1, begin_list2, cmp); + } +} -- cgit