-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush_swap.c
88 lines (80 loc) · 1.4 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
86
87
88
#include "push_swap.h"
void *ft_calloc(size_t count, size_t size)
{
void *ptr;
ptr = malloc(count * size);
if (!ptr)
return (NULL);
ft_bzero(ptr, count * size);
return (ptr);
}
void init_struct(t_ps *ps)
{
ps->bottom_a = ps->size - 1;
ps->top_b = ps->size;
ps->pos = 0;
ps->chunks = 0;
}
void init(t_ps *ps, int *stack_a, int *stack_b, char **argv)
{
int j;
int i;
j = 0;
while (j < ps->size)
{
stack_a[j] = 0;
stack_b[j] = 0;
j++;
}
j = 0;
i = 1;
while (i < ps->size + 1)
{
stack_a[j] = ft_atoi(argv[i]);
i++;
j++;
}
j = 0;
while (j < ps->size + 1)
{
if (stack_a[j] == 0)
stack_a[j] = 1;
j++;
}
}
void check_init(char **argv, t_ps *ps, int *stack_a, int *stack_b)
{
if (check_digit(argv) == -1 || check_dup(argv) == -1)
{
free(stack_a);
free(stack_b);
exit(0);
}
init_struct(ps);
init(ps, stack_a, stack_b, argv);
}
int main(int argc, char **argv)
{
t_ps ps;
int *stack_a;
int *stack_b;
stack_a = (int *)malloc(sizeof(int) * argc - 1);
stack_b = (int *)malloc(sizeof(int) * argc - 1);
ps.size = argc - 1;
check_init(argv, &ps, stack_a, stack_b);
if (check_order(stack_a, &ps) == 1)
exit(0);
if (argc == 4)
org_3dig(stack_a, &ps);
if (argc >= 5)
{
while (ps.bottom_a > 2)
org_stack_a(stack_a, stack_b, &ps);
org_3dig(stack_a, &ps);
while (ps.top_b < ps.size)
org_stack_b(stack_a, stack_b, &ps);
}
free(stack_a);
free(stack_b);
return (0);
}