-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpush_largest.c
128 lines (112 loc) · 2.06 KB
/
push_largest.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
116
117
118
119
120
121
122
123
124
125
126
127
#include "push_swap.h"
//////////////////////////////////////////////////////////////////
/////////////////////////// NEW ALGO /////////////////////////////
//////////////////////////////////////////////////////////////////
int has_biger(t_list *stack, int num)
{
while(stack != NULL)
{
if (stack->data >= num)
return 1;
stack = stack->next;
}
return 0;
}
int get_biger(t_list *stack, int num)
{
t_list *start_top;
t_list *start_bottom;
start_bottom = stack;
start_top = stack;
while (start_bottom->next != NULL)
start_bottom = start_bottom->next;
while(start_top != NULL && start_bottom != NULL)
{
if (start_top->data >= num)
return start_top->data;
else if(start_bottom->data >= num)
return start_bottom->data;
start_top = start_top->next;
start_bottom = start_bottom->prev;
}
return 0;
}
void make_at_top(t_list **stack, int num)
{
int index;
int len;
int top;
if (*stack != NULL)
{
index = index_of(*stack, num);
len = list_len(*stack);
top = (*stack)->data;
if(index <= (len / 2))
{
while (num != top)
{
rb(stack);
top = (*stack)->data;
}
}
else
{
while (num != top)
{
rrb(stack);
top = (*stack)->data;
}
}
}
}
void send_chunck(t_list **stack, t_list **stack_to_push, int more_than)
{
int biger;
int index;
int len;
int top;
while (has_biger(*stack, more_than))
{
biger = get_biger(*stack, more_than);
index = index_of(*stack, biger);
if (index < len/2)
{
while(top != biger)
{
ra(stack);
top = (*stack)->data;
}
}
else
{
while(top != biger)
{
rra(stack);
top = (*stack)->data;
}
}
pb(stack, stack_to_push);
}
}
void sort_sending_biger(t_list **stack_b, t_list **stack_a)
{
int bigest;
while((*stack_b) != NULL)
{
bigest = find_bigest(*stack_b);
make_at_top(stack_b, bigest);
pa(stack_b, stack_a);
ra(stack_a);
}
}
void repeat(t_list **stack_a, t_list **stack_b)
{
int *arr;
int i = 3;
arr = helper(*stack_a);
while(i >= 0)
{
send_chunck(stack_a, stack_b, arr[i]);
sort_sending_biger(stack_b, stack_a);
}
}