-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
73 lines (67 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zachamou <zachamou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/10 14:45:13 by zachamou #+# #+# */
/* Updated: 2023/10/19 21:45:36 by zachamou ### ########.fr */
/* */
/* ************************************************************************** */
#include "inc/push_swap.h"
void init_stack(t_list **stack, int ac, char **params)
{
t_list *new;
int num;
int i;
i = 0;
if (ac == 1)
params = ft_split(params[0], ' ');
while (params[i])
{
ft_atoi_ps(params[i], &num);
new = ft_new_list(num);
ft_list_add_back(stack, new);
i++;
}
get_index(stack);
if (ac == 1)
ft_free(params);
}
void check_list(t_list **stack_a, t_list **stack_b)
{
if (is_sorted(stack_a))
{
ft_free_pile(stack_a);
ft_free_pile(stack_b);
exit(0);
}
if (ft_size_list(*stack_a) <= 5)
sort_1to2(stack_a, stack_b);
else
radix_sort(stack_a, stack_b);
}
int main(int ac, char **av)
{
t_list **a;
t_list **b;
if (ac < 2)
return (EXIT_FAILURE);
if (!check_params(ac - 1, av + 1))
{
ft_error("Error");
exit(1);
}
a = (t_list **)malloc(sizeof(t_list *));
b = (t_list **)malloc(sizeof(t_list *));
if (a == NULL || b == NULL)
exit(1);
*a = NULL;
*b = NULL;
init_stack(a, ac - 1, av + 1);
check_list(a, b);
ft_free_pile(a);
ft_free_pile(b);
return (0);
}