-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap.c
More file actions
79 lines (72 loc) · 2.03 KB
/
push_swap.c
File metadata and controls
79 lines (72 loc) · 2.03 KB
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
/* ************************************************************************** */
/* */
/* :::::::: */
/* push_swap.c :+: :+: */
/* +:+ */
/* By: rcorke <rcorke@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/08/12 12:25:28 by rcorke #+# #+# */
/* Updated: 2019/08/23 19:17:33 by rcorke ######## odam.nl */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static t_ps *create_struct(int argc, char **argv)
{
t_ps *ps;
int x;
ps = (t_ps *)malloc(sizeof(t_ps));
ps->a = (int *)malloc(sizeof(int) * argc);
ps->b = (int *)malloc(sizeof(int) * argc);
ps->check = 0;
ps->clist = NULL;
ps->len_a = argc - 1;
ps->len_b = 0;
x = 0;
ps->ret = 0;
ps->size = argc - 1;
while (x < argc - 1)
{
ps->a[x] = ft_atoi(argv[x + 1]);
ps->b[x] = 0;
x++;
}
ps->print_commands = 1;
x = 0;
return (ps);
}
static void which_algo_small(t_ps *ps)
{
if (ps->size == 1)
return ;
else if (ps->size == 2)
return (check_swap(ps, 'a'));
else if (ps->size == 3)
return (sort_3_a(ps));
else if (ps->size == 4)
return (sort_4(ps));
else
return (small_algo(ps));
}
static int free_everything(t_ps *ps)
{
free(ps->a);
free(ps->b);
free(ps);
return (1);
}
int main(int argc, char **argv)
{
t_ps *ps;
if (argc < 2)
return (1);
ps = create_struct(argc, argv);
if (!find_unordered_ascending(ps->a, ps->len_a))
return (free_everything(ps));
if (argc < 7)
which_algo_small(ps);
else
big_algorithm_three(ps);
check_for_dumb_commands(ps, 0);
print_clist(ps);
return (free_everything(ps));
}