This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstsort.c
executable file
·53 lines (48 loc) · 1.6 KB
/
ft_lstsort.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_lstsort.c :+: :+: */
/* +:+ */
/* By: fbes <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/05/07 16:41:18 by fbes #+# #+# */
/* Updated: 2022/02/08 19:48:05 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static void swap_lst_content(t_list *a, t_list *b)
{
void *temp;
temp = a->content;
a->content = b->content;
b->content = temp;
}
/**
* Sort a linked list
* @param[in] **lst The list to sort
* @param[in] (*f)(void *, void *) The method to apply to sort the list, should
* return 1 if the first item is bigger than
* the second
*/
void ft_lstsort(t_list **lst, int (*f)(void *, void *))
{
size_t size;
t_list *item;
size_t i;
size_t j;
size = ft_lstsize(*lst);
i = 0;
while (i < size - 1)
{
j = 0;
item = *lst;
while (j < size - i - 1)
{
if ((*f)(item->content, item->next->content))
swap_lst_content(item, item->next);
item = item->next;
j++;
}
i++;
}
}