-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpile.c
94 lines (85 loc) · 2.27 KB
/
pile.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pile.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cgleason <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/03 13:51:54 by cgleason #+# #+# */
/* Updated: 2018/08/03 13:52:05 by cgleason ### ########.fr */
/* */
/* ************************************************************************** */
#include "swap_push.h"
int stcklst_len(t_dblstck **stck_array)
{
int i;
i = 0;
while (stck_array[i] != NULL)
i++;
return (i);
}
void heap_repair(t_dblstck **stck_array, int ind, int n)
{
int lind;
int rind;
int maxind;
t_dblstck *tmp;
lind = 2 * ind + 1;
rind = 2 * ind + 2;
maxind = ind;
if (lind < n)
if (stck_array[lind]->num > stck_array[maxind]->num)
maxind = lind;
if (rind < n)
if (stck_array[rind]->num > stck_array[maxind]->num)
maxind = rind;
if (maxind != ind)
{
tmp = stck_array[ind];
stck_array[ind] = stck_array[maxind];
stck_array[maxind] = tmp;
heap_repair(stck_array, maxind, n);
}
}
void stck_heapsort(t_dblstck **stck_array)
{
int n;
t_dblstck *tmp;
int i;
n = stcklst_len(stck_array);
i = n / 2 + 1;
while (i--)
heap_repair(stck_array, i, n);
i = n;
while (i--)
{
tmp = stck_array[i];
stck_array[i] = stck_array[0];
stck_array[0] = tmp;
heap_repair(stck_array, 0, i);
}
}
int convert_rank(t_dblstck *stck)
{
t_dblstck **stck_array;
int i;
t_dblstck *tmp;
tmp = stck->next;
i = 1;
while (tmp != stck && i++)
tmp = tmp->next;
stck_array = (t_dblstck **)ft_memalloc((i + 1) * sizeof(t_dblstck *));
if (stck_array == NULL)
return (0);
while (i--)
{
stck_array[i] = tmp;
tmp = tmp->next;
}
stck_heapsort(stck_array);
i = -1;
while (stck_array[++i] != NULL)
stck_array[i]->num = i;
free(stck_array);
return (i);
}