forked from sisittu99/push_swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
83 lines (76 loc) · 2.05 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcerchi <mcerchi@student.42roma.it> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/12 10:53:52 by mcerchi #+# #+# */
/* Updated: 2022/02/26 14:49:44 by mcerchi ### ########.fr */
/* */
/* ************************************************************************** */
#include "./libft/libft.h"
#include "push_swap.h"
void ft_lst_split(t_list **stack_a, t_list **stack_b, int size)
{
int *dst;
int *arr;
int i;
i = 0;
if (size == 3)
{
ft_sort_three(stack_a);
return ;
}
if (size == 5)
{
ft_sort_five(stack_a, stack_b);
return ;
}
dst = ft_copy_cont(*stack_a, size);
arr = ft_define_lis(dst, size, &i);
ft_move_to_b(stack_a, stack_b, arr, i);
ft_move_to_a(stack_a, stack_b);
ft_search_min(stack_a, size);
free (dst);
free (arr);
}
void ft_check_argv(int argc, char **argv, t_list **stack_a)
{
int size;
char **arg;
arg = NULL;
size = 0;
if (argc == 2)
{
arg = ft_split(argv[1], ' ');
while (arg[size] != NULL)
size++;
ft_write_lst(stack_a, size, arg, 0);
free(arg);
}
else if (argc >= 3)
ft_write_lst(stack_a, argc, argv, 1);
}
int main(int argc, char **argv)
{
t_list *stack_a;
t_list *stack_b;
int size;
char **arg;
stack_a = NULL;
stack_b = NULL;
arg = NULL;
size = 0;
if (argc < 2)
return (0);
else
ft_check_argv(argc, argv, &stack_a);
size = ft_lstsize(stack_a);
if (size == 2)
return (0);
ft_lst_split(&stack_a, &stack_b, size);
ft_lst_delete(&stack_a);
ft_lst_delete(&stack_b);
return (0);
}