-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
85 lines (78 loc) · 2.2 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hramaros <hramaros@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 07:38:14 by hramaros #+# #+# */
/* Updated: 2024/07/15 09:26:43 by hramaros ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void big_sort(t_pile **a)
{
t_pile **b;
b = (t_pile **)malloc(sizeof(t_pile *));
if (!b)
return ;
*b = NULL;
put_in_b(a, b);
fix_first_members(*b);
fix_last_members(*b);
mini_sort(a);
send_back_to_a(a, b);
last_round(a, get_min_value_in(*a));
free_pile(a);
free_pile(b);
}
/**
* @brief mini_sort si la pile a moins de 3 elements,
big_sort si la pile a n > 3 elements
*
* @param a
* @date 2024-06-10
*/
void push_swap(t_pile **a)
{
size_t pile_size;
pile_size = get_pile_size(*a);
if (pile_size == 2)
{
ra(a);
free_pile(a);
}
else if (pile_size == 3)
{
mini_sort(a);
free_pile(a);
}
else
big_sort(a);
}
int main(int argc, char **argv)
{
t_pile **a;
int i;
a = (t_pile **)malloc(sizeof(t_pile *));
if (!a)
return (write(2, "Erreur d'allocation de la pile\n", 31), 1);
if (!(argc >= 2) && (argc != 1))
return (free(a), render_exit(), 1);
if (argc == 2)
argc = get_args_numbers(argv[1]);
else if (argc == 1)
return (free(a), 0);
else
argc -= 1;
i = 1;
while (argv[i])
if (!ft_isnumber(argv[i++]))
return (free(a), render_exit(), 1);
*a = verify_argv(argv);
if (!*a || ft_has_duplicates(*a) || ft_has_greater_than(*a, INT_MAX))
return (free_pile(a), render_exit(), 0);
if (ft_is_sorted(*a) || (*a == NULL))
return (free_pile(a), 0);
return (push_swap(a), 0);
}