-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
60 lines (55 loc) · 1.75 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zmahmoud <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 10:50:15 by zmahmoud #+# #+# */
/* Updated: 2022/07/17 18:06:50 by zmahmoud ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void sort(int argc, t_stack **stack)
{
if (argc == 2)
ft_sa(*stack, 1);
else if (argc == 3)
sort_three_numbers(stack);
else if (argc <= 10)
sort_less_ten(stack);
else
big_sort(stack, argc);
}
static void ft_free_args(char **ret, int i)
{
while (i > 0)
free(ret[--i]);
free(ret);
}
int main(int argc, char *argv[])
{
int i;
t_stack *stack;
char **args;
char *args_str;
args_str = args_to_str(argc, argv);
args = ft_split(args_str, ' ');
argc = arglen(args);
if (argc <= 1)
ft_check_args(argc, args);
else if (argc > 1)
{
ft_check_args(argc, args);
ft_check_duplicated(argc, args);
stack = ft_lstnew(ft_atoi(args[0]));
i = 1;
while (i < argc)
ft_lstadd_back(&stack, ft_lstnew(ft_atoi(args[i++])));
if (!is_sorted(stack))
sort(argc, &stack);
}
else if (argc == 2)
ft_check_args(argc, args);
ft_free_args(args, argc);
}