-
Notifications
You must be signed in to change notification settings - Fork 0
/
minizer.c
115 lines (106 loc) · 2.74 KB
/
minizer.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
103
104
105
106
107
108
109
110
111
112
113
114
115
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minizer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kotainou <kotainou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/21 19:39:17 by kotainou #+# #+# */
/* Updated: 2023/09/27 12:52:46 by kotainou ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void stack_a_loaf_array(t_double_stack *head_stack, int *thr_stack)
{
t_linked_tag *p;
int i;
i = 0;
p = head_stack->stack_a->next;
// printf("sett ");
while (p != head_stack->stack_a)
{
p->value = thr_stack[i] + 1;
// printf("%d ", p->value);
i++;
p = p->next;
}
free(thr_stack);
// printf("\n");
}
void two_to_thr(t_double_stack *head_stack, int *one_stack, int *two_stack)
{
int *thr_stack;
int i;
int j;
thr_stack = ft_calloc(sizeof(int *), (head_stack->tail_a + 1));
i = 0;
while (i < head_stack->tail_a)
{
j = 0;
while (j < head_stack->tail_a)
{
if (one_stack[i] == two_stack[j])
{
// printf("%d ", two_stack[j]);
thr_stack[i] = j;
break ;
}
j++;
}
i++;
}
free(one_stack);
free(two_stack);
stack_a_loaf_array(head_stack, thr_stack);
}
//重複の処理もするところ
void bubble_sort(t_double_stack *head_stack, int *one_stack)
{
int *two_stack;
int i;
int j;
int tmp;
two_stack = ft_calloc(sizeof(int *), (head_stack->tail_a + 1));
ft_memcpy(two_stack, one_stack, sizeof(int *) * head_stack->tail_a);
i = 0;
while (i < head_stack->tail_a)
{
j = i + 1;
while (j < head_stack->tail_a)
{
if (two_stack[i] > two_stack[j])
{
tmp = two_stack[i];
two_stack[i] = two_stack[j];
two_stack[j] = tmp;
}
else if (two_stack[i] == two_stack[j])
{
ft_printf("Error\n");
exit(1);
}
j++;
}
i++;
}
two_to_thr(head_stack, one_stack, two_stack);
}
void minizer_stack(t_double_stack *head_stack)
{
t_linked_tag *p;
size_t i;
int *one_stack;
one_stack = ft_calloc(sizeof(int *), (head_stack->tail_a + 1));
p = head_stack->stack_a->next;
i = 0;
// printf("mini\n");
while (p != head_stack->stack_a)
{
one_stack[i] = p->value;
// printf("%d ", p->value);
p = p->next;
i++;
}
// printf("mini\n");`
bubble_sort(head_stack, one_stack);
}