aboutsummaryrefslogtreecommitdiff
path: root/c12/ex17
diff options
context:
space:
mode:
Diffstat (limited to 'c12/ex17')
-rw-r--r--c12/ex17/ft_list.h4
-rw-r--r--c12/ex17/ft_sorted_list_merge.c47
2 files changed, 49 insertions, 2 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/07/22 08:13:44 by cacharle #+# #+# */
+/* Updated: 2019/07/23 16:09:19 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+#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);
+ }
+}