-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_lst_functions.c
89 lines (80 loc) · 1.89 KB
/
ft_lst_functions.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lst_functions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iyapar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/29 15:07:31 by iyapar #+# #+# */
/* Updated: 2022/06/30 16:49:38 by iyapar ### ########.fr */
/* */
/* ************************************************************************** */
#include "header.h"
static t_struct *get_next_min(t_struct **stackA)
{
t_struct *head;
t_struct *min;
int flag;
head = *stackA;
flag = 0;
min = NULL;
if (!head)
return (NULL);
while (head)
{
if ((head->index == -1) && (!flag || head->value < min->value))
{
flag = 1;
min = head;
}
head = head->next;
}
return (min);
}
void add_index(t_struct **stackA)
{
t_struct *head;
int i;
i = 0;
head = get_next_min(stackA);
while (head)
{
head->index = i++;
head = get_next_min(stackA);
}
}
t_struct *ft_lst_fill(char **av, int nmbr)
{
int i;
t_struct *head;
t_struct *temp;
i = 1;
head = NULL;
while (i < nmbr)
{
temp = ft_lstnew(ft_atoi(av[i]));
if (temp == NULL)
{
ft_lst_free(&head);
break ;
}
ft_lstadd_back(&head, temp);
i++;
}
add_index(&head);
return (head);
}
void ft_lst_free(t_struct **stackA)
{
t_struct *temp;
temp = NULL;
while (*stackA)
{
temp = (*stackA)->next;
free(*stackA);
*stackA = NULL;
if (temp == NULL)
break ;
*stackA = temp;
}
}