-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortinstr.c
More file actions
93 lines (83 loc) · 2.33 KB
/
sortinstr.c
File metadata and controls
93 lines (83 loc) · 2.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sortinstr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgerda <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/23 19:03:08 by bgerda #+# #+# */
/* Updated: 2020/02/23 19:05:18 by bgerda ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void first_b(t_stack *stack)
{
t_node *tmp;
int range;
tmp = stack->head;
range = tmp->range;
if (tmp->data < tmp->next->data)
s_ab(stack);
else if (tmp->data > tmp->next->data)
{
r_ab(stack, range);
s_ab(stack);
rr_ab(stack, range);
s_ab(stack);
}
}
void middle_b(t_stack *stack)
{
t_node *tmp;
int range;
tmp = stack->head;
range = tmp->range;
s_ab(stack);
r_ab(stack, range);
s_ab(stack);
rr_ab(stack, range);
s_ab(stack);
}
void last_b(t_stack *stack)
{
t_node *tmp;
int range;
tmp = stack->head;
range = tmp->range;
if (tmp->data < tmp->next->data)
{
s_ab(stack);
r_ab(stack, range);
s_ab(stack);
rr_ab(stack, range);
}
}
void select_stack(t_stack *stack, int num)
{
if (stack->prop->name == 'a' && num == 1)
first_a(stack);
else if (stack->prop->name == 'b' && num == 1)
first_b(stack);
else if (stack->prop->name == 'a' && num == 2)
middle_a(stack);
else if (stack->prop->name == 'b' && num == 2)
middle_b(stack);
else if (stack->prop->name == 'a' && num == 3)
last_a(stack);
else if (stack->prop->name == 'b' && num == 3)
last_b(stack);
}
void instr_sort(t_stack *stack)
{
int midpoint;
t_node *tmp;
tmp = stack->head;
midpoint = head_part(stack);
if (tmp->data == midpoint)
select_stack(stack, 1);
else if (tmp->next->data == midpoint &&
tmp->data > tmp->next->next->data)
select_stack(stack, 2);
else if (tmp->next->next->data == midpoint)
select_stack(stack, 3);
}