-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_sort.c
executable file
·73 lines (69 loc) · 2.28 KB
/
list_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 19:26:23 by akharrou #+# #+# */
/* Updated: 2019/06/08 14:05:49 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_sort -- sort a list according to a comparasion function.
**
** SYNOPSIS
** #include <../libft.h>
**
** void
** list_sort(t_list **head, int (*cmp)(const void *, const void *));
**
** PARAMETERS
**
** t_list **head Pointer to a pointer to the
** first element of a list
**
** int (*cmp)(void *, void *) A comparasion function to
** compare two items.
**
** Returns:
** - >= 1 -- if a > b
** - 0 -- if a == b
** - < 0 -- if a < b
**
** DESCRIPTION
** Sorts the elements of a list according to a comparasion
** function.
**
** RETURN VALUES
** None.
*/
#include "../Includes/list.h"
void list_sort(t_list **head, int (*cmp)(const void *, const void *))
{
unsigned int i;
unsigned int size;
void *temp;
t_list *probe;
if (head && (*head))
{
size = list_count(*head);
while (size > 0)
{
i = 0;
probe = (*head);
while (++i < size)
{
if ((*cmp)(probe->item, probe->next->item) > 0)
{
temp = probe->item;
probe->item = probe->next->item;
probe->next->item = temp;
}
probe = probe->next;
}
size--;
}
}
}