-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
95 lines (87 loc) · 2.35 KB
/
main.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
89
90
91
92
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nmaliare <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/23 15:15:09 by nmaliare #+# #+# */
/* Updated: 2023/02/22 18:57:18 by nmaliare ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_check_dupl(int num, t_data *start, t_dlist **lst)
{
int len;
len = start->len;
*lst = start->head;
while (--len > 0)
{
if (num == (*lst)->num)
return (0);
(*lst) = (*lst)->next;
}
if (num == (*lst)->num)
return (0);
return (1);
}
void clean_exit(t_pushswap *all, char f)
{
if (all->a)
ft_circle_free(&all->a, all->astart);
if (all->b)
ft_circle_free(&all->b, all->bstart);
free(all->astart);
free(all->bstart);
if (f == 'm')
ft_printf("Malloc Error!\n");
else if (f == 'd')
ft_printf("Error\n");
exit (1);
}
void init_all(t_pushswap *all)
{
t_dlist *a;
t_dlist *b;
t_data *astart;
t_data *bstart;
a = 0;
b = 0;
astart = ft_calloc(sizeof(t_data), 1);
bstart = ft_calloc(sizeof(t_data), 1);
if (!astart || !bstart)
exit(1);
astart->head = a;
bstart->head = b;
astart->len = 0;
bstart->len = 0;
all->a = a;
all->b = b;
all->astart = astart;
all->bstart = bstart;
}
int main(int ac, char **av)
{
t_pushswap all;
int i;
int num;
i = ac - 1;
if (ac < 2)
return (0);
if (!err_input(ac, av) || (ac == 2 && !err_input(ac, av)))
return (0 & ft_printf("Error\n"));
init_all(&all);
all.a = ft_circle_newnode(ft_atoi(av[i]), 0, &all);
all.astart->len = 1;
all.astart->head = all.a;
while (--i > 0)
{
num = ft_atoi(av[i]);
if (!ft_check_dupl(num, all.astart, &(all.a)))
clean_exit(&all, 'd');
ft_circle_addfront(&(all.a), \
ft_circle_newnode(num, 0, &all), all.astart);
}
ft_prepare(&all);
return (0);
}