-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindfunctions.c
87 lines (79 loc) · 2 KB
/
findfunctions.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* findfunctions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hde-vos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/08/29 10:03:50 by hde-vos #+# #+# */
/* Updated: 2019/09/12 12:37:40 by hde-vos ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *find_weight(t_stack *stack, int n)
{
while (stack->weight != n)
stack = stack->next;
return (stack);
}
t_stack *find_top(t_stack *stack)
{
while (stack->prev)
stack = stack->prev;
return (stack);
}
t_stack *find_bottom(t_stack *stack)
{
while (stack->next)
{
stack = stack->next;
}
return (stack);
}
t_stack *find_max(t_stack *stack)
{
// lose i to save some lines
t_stack *ret;
t_stack *stack_temp;
int i;
i = stack->weight;
stack_temp = stack;
ret = stack;
while (stack_temp)
{
if (stack_temp->end == 1)
stack_temp->end = 0;
if (stack_temp->weight > i)
{
ret = stack_temp;
i = stack_temp->weight;
}
stack_temp = stack_temp->next;
}
stack_temp = stack;
ret->end = 1;
return (ret);
}
t_stack *find_min(t_stack *stack)
{
t_stack *ret;
t_stack *stack_temp;
int i;
i = stack->weight;
stack_temp = stack;
ret = stack;
while (stack_temp)
{
if (stack_temp->end == -1)
stack_temp->end = 0;
if (stack_temp->weight < i)
{
ret = stack_temp;
i = stack_temp->weight;
}
stack_temp = stack_temp->next;
}
stack_temp = stack;
ret->end = -1;
return (ret);
}