-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_l_i_s.c
80 lines (73 loc) · 2.06 KB
/
init_l_i_s.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_l_i_s.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: shaas <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/04 20:39:24 by shaas #+# #+# */
/* Updated: 2022/03/09 00:17:24 by shaas ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void assign_l_i_s(t_list *stack_a)
{
t_node *search;
t_node *iter;
iter = stack_a->tail;
while (iter != NULL)
{
iter->l_i_s_length = 1;
iter->l_i_s_next = NULL;
search = iter->next;
while (search != NULL)
{
if (search->rank > iter->rank && (iter->l_i_s_next == NULL || \
search->l_i_s_length > iter->l_i_s_next->l_i_s_length))
{
iter->l_i_s_length = 1 + search->l_i_s_length;
iter->l_i_s_next = search;
}
search = search->next;
}
iter = iter->prev;
}
}
static t_node *find_l_i_s(t_list *stack_a)
{
t_node *iter;
t_node *l_i_s;
assign_l_i_s(stack_a);
l_i_s = stack_a->head;
iter = stack_a->head;
while (iter != NULL)
{
if (iter->l_i_s_length > l_i_s->l_i_s_length)
l_i_s = iter;
iter = iter->next;
}
return (l_i_s);
}
void init_l_i_s(t_list *stack_a, t_list *stack_b, unsigned int numnum)
{
t_node *iter;
t_node *next;
t_node *l_i_s;
unsigned int i;
i = 0;
iter = stack_a->head;
l_i_s = find_l_i_s(stack_a);
while (i < numnum)
{
next = iter->next;
if (iter == l_i_s)
{
rotate_a(stack_a, stack_b);
l_i_s = l_i_s->l_i_s_next;
}
else
push_b(stack_a, stack_b);
iter = next;
i++;
}
}