-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_swap.c
114 lines (103 loc) · 2.3 KB
/
push_swap.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mohkhald <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/18 22:44:31 by mohkhald #+# #+# */
/* Updated: 2025/04/07 09:37:14 by mohkhald ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_add_back(t_stack **stack, int n)
{
t_stack *new;
t_stack *tmp;
new = malloc(sizeof(t_stack));
if (!new)
{
ft_print_error(stack);
}
new->value = n;
new->next = NULL;
tmp = *stack;
if (!*stack)
*stack = new;
else
{
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
}
void ft_process_number(char *num_str, t_stack **a, char **split)
{
long num;
if (!num_str || ft_check_input(num_str))
{
ft_free_stack(split);
ft_print_error(a);
}
num = ft_atoi(num_str);
if (num < INT_MIN || num > INT_MAX || ft_duplicate(*a, num))
{
ft_free_stack(split);
ft_print_error(a);
}
ft_add_back(a, num);
}
void ft_process_split(char **split, t_stack **a)
{
int j;
j = 0;
if (!split[0])
{
ft_free_stack(split);
ft_print_error(a);
}
while (split[j])
{
ft_process_number(split[j], a, split);
j++;
}
}
void ft_parse_inp(char **s, t_stack **a)
{
int i;
char **split;
i = 1;
if (s)
{
while (s[i])
{
if (!s[i] || *s[i] == '\0')
ft_print_error(a);
split = ft_split(s[i], ' ');
if (!split)
ft_print_error(a);
ft_process_split(split, a);
ft_free_stack(split);
i++;
}
}
}
int main(int ac, char **av)
{
t_stack *a;
t_stack *b;
b = NULL;
a = NULL;
if (ac == 1)
return (0);
if (ac > 1)
ft_parse_inp(av, &a);
if (a && !ft_is_sorted(a))
{
ft_sort_stack(&a, &b);
ft_move_larg_to_a(&a, &b);
}
ft_free_list(&a);
ft_free_list(&b);
return (0);
}