-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_init.c
96 lines (88 loc) · 2.18 KB
/
stack_init.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/30 15:09:28 by sanmetol #+# #+# */
/* Updated: 2024/10/02 16:02:23 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_node *find_last_node(t_node *top)
{
if (top == NULL)
return (NULL);
while (top->next)
top = top->next;
return (top);
}
static void append_node(t_node **stack, int nbr)
{
t_node *node;
t_node *last_node;
if (stack == NULL)
return ;
node = malloc(sizeof(t_node));
if (node == NULL)
return ;
node->next = NULL;
node->n = nbr;
node->cheapest = false;
if (*stack == NULL)
{
*stack = node;
node->prev = NULL;
}
else
{
last_node = find_last_node(*stack);
last_node->next = node;
node->prev = last_node;
}
}
static long ft_atol(const char *str)
{
long n;
int i;
int neg;
i = 0;
n = 0;
neg = 1;
while ((str[i] == ' ' || str[i] == '\f' || str[i] == '\n'
|| str[i] == '\r' || str[i] == '\t' || str[i] == '\v')
&& str[i])
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
neg = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
n = str[i] - '0' + n * 10;
i++;
}
return (n * neg);
}
void stack_init(t_node **a, char **argv)
{
long nbr;
int i;
i = 0;
while (argv[i])
{
if (error_syntax(argv[i]))
free_error(a, argv);
nbr = ft_atol(argv[i]);
if (nbr > INT_MAX || nbr < INT_MIN)
free_error(a, argv);
if (error_repetition(*a, (int)nbr))
free_error(a, argv);
append_node(a, (int)nbr);
i++;
}
free_matrix(argv);
}