-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_init.c
More file actions
112 lines (101 loc) · 2.41 KB
/
Copy pathstack_init.c
File metadata and controls
112 lines (101 loc) · 2.41 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tbui-quo <tbui-quo@student.42wolfsburg.d> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/23 17:39:15 by tbui-quo #+# #+# */
/* Updated: 2023/08/23 17:39:15 by tbui-quo ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack_data_set *init_stacks_struct(void)
{
t_stack_data_set *set;
set = malloc(sizeof(t_stack_data_set));
if (!set)
return (NULL);
set->stack_a = NULL;
set->stack_b = NULL;
return (set);
}
t_stack *create_stack_a(char **input)
{
t_stack *stack_a;
char **input_array;
size_t index;
stack_a = NULL;
input_array = input;
index = 0;
while (input_array[index])
{
if (insert_stack(&stack_a, ft_atoi(input_array[index])) == false)
{
stack_clear(&stack_a);
return (NULL);
}
index++;
}
index_stack(&stack_a);
return (stack_a);
}
bool insert_stack(t_stack **root, int value)
{
t_stack *current;
t_stack *new_node;
current = NULL;
new_node = malloc(sizeof(t_stack));
if (new_node == NULL)
{
return (false);
}
new_node->content = value;
new_node->index = -1;
new_node->next = NULL;
if (*root == NULL)
*root = new_node;
else
{
current = *root;
while (current->next != NULL)
current = current->next;
current->next = new_node;
}
return (true);
}
t_stack *get_next_min_node(t_stack *stack)
{
t_stack *root;
t_stack *min;
bool has_min;
root = stack;
min = NULL;
has_min = false;
if (root)
{
while (root)
{
if ((root->index == -1) && ((!has_min)
|| (root->content < min->content)))
{
min = root;
has_min = true;
}
root = root->next;
}
}
return (min);
}
void index_stack(t_stack **stack)
{
int index;
t_stack *root;
index = 0;
root = get_next_min_node(*stack);
while (root)
{
root->index = ++index;
root = get_next_min_node(*stack);
}
}