-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_init.c
More file actions
115 lines (106 loc) · 2.45 KB
/
stack_init.c
File metadata and controls
115 lines (106 loc) · 2.45 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joamonte <joamonte@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 12:04:14 by joamonte #+# #+# */
/* Updated: 2024/03/27 10:58:39 by joamonte ### ########.fr */
/* */
/* ************************************************************************** */
#include "./push_swap.h"
static long ft_atol(const char *s)
{
long result;
int sign;
result = 0;
sign = 1;
while (*s == ' ' || *s == '\t' || *s == '\n' || \
*s == '\r' || *s == '\f' || *s == '\v')
s++;
if (*s == '-' || *s == '+')
{
if (*s == '-')
sign = -1;
s++;
}
while (ft_isdigit(*s))
result = result * 10 + (*s++ - '0');
return (result * sign);
}
static void new_node(t_stack_node **stack, int n)
{
t_stack_node *node;
t_stack_node *last_node;
if (!stack)
return ;
node = ft_calloc(1, sizeof(t_stack_node));
if (!node)
return ;
node->next = NULL;
node->nbr = n;
if (!(*stack))
{
*stack = node;
node->prev = NULL;
}
else
{
last_node = find_last(*stack);
last_node->next = node;
node->prev = last_node;
}
}
void init_stack_a(t_stack_node **a, char **av)
{
long n;
int i;
i = 0;
while (av[i])
{
if (check_syntax(av[i]))
free_errors(a);
n = ft_atol(av[i]);
if (n > INT_MAX || n < INT_MIN)
free_errors(a);
if (check_duplicate(*a, (int)n))
free_errors(a);
new_node(a, (int)n);
i++;
}
}
t_stack_node *get_cheapest(t_stack_node *stack)
{
if (!stack)
return (NULL);
while (stack)
{
if (stack->cheapest)
return (stack);
stack = stack->next;
}
return (NULL);
}
void prep_for_push(t_stack_node **stack,
t_stack_node *top_node,
char stack_name)
{
while (*stack != top_node)
{
if (stack_name == 'a')
{
if (top_node->above_mid)
ra(stack, false);
else
rra(stack, false);
}
else if (stack_name == 'b')
{
if (top_node->above_mid)
rb(stack, false);
else
rrb(stack, false);
}
}
}