-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
102 lines (91 loc) · 2.39 KB
/
list.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
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joalmeid <joalmeid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 10:44:10 by joalmeid #+# #+# */
/* Updated: 2023/03/09 12:02:14 by joalmeid ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_list *ft_node_new_data(int data, int index)
{
t_list *new_node;
new_node = malloc(sizeof(t_list));
if (new_node == NULL)
return (NULL);
new_node->data = data;
new_node->index = index;
new_node->next = NULL;
return (new_node);
}
void create_stack_a(t_list **stack_a, char **argv)
{
int i;
int j;
i = 0;
while (argv[i])
{
ft_lstadd_back(stack_a, ft_node_new_data(ft_atoi(argv[i]), 0));
i ++;
}
j = i;
i = 1;
while (j > 1 && i <= j)
{
put_index_node(stack_a, i);
i ++;
}
}
void put_index_node(t_list **stack, int index)
{
t_list *first;
int minor_data;
first = *stack;
minor_data = find_minor_data(stack);
while (*stack && (minor_data != (*stack)->data))
*stack = (*stack)->next;
if (*stack != NULL)
(*stack)->index = index;
*stack = first;
}
int find_minor_data(t_list **stack)
{
t_list *first;
int minor_data;
first = *stack;
minor_data = first->data;
while (*stack != NULL && (*stack)->index != 0)
*stack = (*stack)->next;
if (*stack == NULL)
return (-1);
minor_data = (*stack)->data;
while (*stack != NULL)
{
if (minor_data > (*stack)->data && (*stack)->index == 0)
minor_data = (*stack)->data;
*stack = (*stack)->next;
}
*stack = first;
return (minor_data);
}
void free_stacks(t_list **stack_a, t_list **stack_b)
{
t_list *tmp;
tmp = NULL;
while (*stack_a != NULL)
{
tmp = (*stack_a)->next;
free((*stack_a));
(*stack_a) = tmp;
}
tmp = NULL;
while (*stack_b != NULL)
{
tmp = (*stack_b)->next;
free((*stack_b));
(*stack_b) = tmp;
}
}