-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_swap.c
More file actions
85 lines (78 loc) · 2.16 KB
/
push_swap.c
File metadata and controls
85 lines (78 loc) · 2.16 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
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: opopov <opopov@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/13 11:04:00 by opopov #+# #+# */
/* Updated: 2025/03/27 10:46:13 by opopov ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void main_stack_operation(t_stack **stack, int buff)
{
t_stack *new_node;
t_stack *last_node;
new_node = malloc(sizeof(t_stack));
if (!new_node)
return ;
new_node->next = NULL;
new_node->value = buff;
new_node->is_best_move_choice = 0;
if (!(*stack))
{
*stack = new_node;
new_node->previous = NULL;
}
else
{
last_node = find_last(*stack);
last_node->next = new_node;
new_node->previous = last_node;
}
}
int create_stack(char **argv, t_stack **stack)
{
long buff;
int i;
if (!ft_str_isdigit(argv))
return (1);
if (!ft_duplicate_check(argv))
return (1);
i = -1;
while (argv[++i])
{
buff = ft_atol(argv[i]);
if (buff > INT_MAX || buff < INT_MIN)
return (1);
main_stack_operation(stack, (int)buff);
}
return (0);
}
int main (int argc, char **argv)
{
t_stack *a = NULL;
t_stack *b = NULL;
int count;
if (argc < 2 || (argc == 2 && !argv[1][0]))
return (ft_printf("Error\n"));
if (argc == 2)
argv = split(argv[1], ' ');
if (create_stack(argv + 1, &a))
{
free_stack(&a);
return (ft_printf("Error\n"));
}
count = count_stacks(a);
if (count == 2)
sort_2(&a);
else if (count == 3)
sort_3(&a);
else if (count >= 4 && count <= 5)
sort_4_and_5(&a, &b);
else if (count > 5)
sort_stacks(&a, &b);
free_stack(&a);
return (0);
}