-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lst_sort.c
57 lines (51 loc) · 1.51 KB
/
ft_lst_sort.c
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
48
49
50
51
52
53
54
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jraymond <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/22 08:40:59 by jraymond #+# #+# */
/* Updated: 2018/05/10 00:59:10 by jraymond ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdio.h>
t_list *ft_swap(t_list **beginlst, t_list *elem, t_list *parent)
{
t_list *tmp;
tmp = elem->next;
elem->next = tmp->next;
tmp->next = elem;
if (parent)
parent->next = tmp;
else
*beginlst = tmp;
return (*beginlst);
}
/*
** cmp : if cmp > 0 i swap;
*/
t_list *ft_lst_sort(t_list *b_list, int (cmp)(t_list *))
{
t_list *elem;
t_list *parent;
elem = b_list;
if (b_list == NULL)
return (NULL);
parent = NULL;
while (elem->next)
{
if (cmp(elem) < 0)
{
elem = ft_swap(&b_list, elem, parent);
parent = NULL;
}
else
{
parent = elem;
elem = elem->next;
}
}
return (b_list);
}