-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_insert.c
executable file
·97 lines (91 loc) · 3.15 KB
/
list_insert.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_insert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/22 19:25:52 by akharrou #+# #+# */
/* Updated: 2019/06/08 14:05:49 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** list_insert -- inserts a new list element where it belongs in a list
** according to the comparasion function.
**
** SYNOPSIS
** #include <../libft.h>
**
** int
** list_insert(t_list **head, const void *item,
** int (*cmp)(const void *, const void *));
**
** PARAMETERS
**
** t_list **head Pointer to a pointer to the
** first element of a list.
**
** const void *item Data that will be stored in
** the new list element.
**
** int (*cmp)(void *, void *) A pointer to a comparasion
** function. It compares the
** item reference to the current
** item. Returns 0 for a match.
**
** DESCRIPTION
** Creates a new list element, storing 'item' as its item, then
** traverses the list until the comparative function returns something
** smaller or equal to 0, at which point the new list element is
** inserted into the list where ever it happened to stop.
**
** If the list does not exist the newly created element is made
** to be the first element of the list.
**
** (*head) is made to point to the first element of the list.
**
** RETURN VALUES
** Returns 0 if successful; otherwise -1.
*/
#include "../Includes/list.h"
static void list_insert_elem(t_list **head, t_list **new_elem,
int (*cmp)(const void *, const void *))
{
t_list *current;
t_list *previous;
current = (*head);
while (cmp((void *)((*new_elem)->item), current->item) > 0)
{
if (current->next)
{
previous = current;
current = current->next;
}
else
{
current->next = (*new_elem);
return ;
}
}
(*new_elem)->next = current;
if (current == (*head))
(*head) = (*new_elem);
else
previous->next = (*new_elem);
return ;
}
int list_insert(t_list **head, const void *item,
int (*cmp)(const void *, const void *))
{
t_list *new_elem;
if (head && cmp && (new_elem = list_newelem(item)))
{
if (*head)
list_insert_elem(head, &new_elem, cmp);
else
(*head) = new_elem;
return (0);
}
return (-1);
}