aboutsummaryrefslogtreecommitdiff
path: root/c12/ex17/ft_sorted_list_merge.c
blob: fd19f36f7ba7a4e427e7ed4a4427a07e04d2ca7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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);
	}
}