-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap.c
100 lines (91 loc) · 2.24 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
89
90
91
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: togauthi <togauthi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/29 10:08:26 by togauthi #+# #+# */
/* Updated: 2024/11/20 14:17:31 by togauthi ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int include(t_stack *stack, int nbr)
{
t_element *current;
current = stack->top;
while (current)
{
if (current->nbr == nbr)
return (1);
current = current->next;
}
return (0);
}
int create_elements(t_stack *stack, char **str)
{
int i;
t_element *element;
i = 0;
while (str[i])
{
element = ft_calloc(sizeof(t_element), 1);
if (!element || include(stack, ft_atoi(str[i])))
{
if (element)
free(element);
free_stack(stack);
free_split(str);
return (0);
}
element->nbr = ft_atoi(str[i]);
element->prev = NULL;
element->next = NULL;
element->index = 0;
put_stack_end(stack, element);
i++;
}
free_split(str);
return (1);
}
t_stack *create_stack(int argc, char **args)
{
t_stack *res;
int i;
res = ft_calloc(sizeof(t_stack), 1);
if (!res)
return (NULL);
i = 0;
while (++i < argc)
{
if (!create_elements(res, ft_split(args[i], ' ')))
return (NULL);
}
return (res);
}
int main(int argc, char **argv)
{
t_stack *stack;
t_stack *stack_2;
if (!check_args(argc, argv))
{
write(2, "Error\n", 6);
return (1);
}
stack = create_stack(argc, argv);
if (!stack)
{
write(2, "Error\n", 6);
return (1);
}
stack_2 = ft_calloc(sizeof(t_stack), 1);
if (!stack_2)
{
free_stack(stack);
write(2, "Error\n", 6);
return (1);
}
sort(stack, stack_2);
free_stack(stack);
free_stack(stack_2);
}