-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
90 lines (81 loc) · 1.89 KB
/
utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dolvin17 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/07 13:03:24 by dolvin17 #+# #+# */
/* Updated: 2023/12/14 11:58:19 by dolvin17 ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void checking(bool if_error, char *str)
{
if (if_error)
{
write(1, str, 7);
exit(EXIT_FAILURE);
}
}
bool is_sorted(t_stack *stack)
{
if (!stack)
return (1);
while (stack->next)
{
if (stack->data > stack->next->data)
return (false);
stack = stack->next;
}
return (true);
}
t_stack *max_value(t_stack *stack)
{
long max_value;
t_stack *max_index;
if (!stack)
return (NULL);
max_value = LONG_MIN;
while (stack)
{
if (stack->data > max_value)
{
max_value = stack->data;
max_index = stack;
}
stack = stack->next;
}
return (max_index);
}
t_stack *min_value(t_stack *stack)
{
long min_value;
t_stack *min_index;
if (!stack)
return (NULL);
min_value = LONG_MAX;
while (stack)
{
if (stack->data < min_value)
{
min_value = stack->data;
min_index = stack;
}
stack = stack->next;
}
return (min_index);
}
int check_len(t_stack *stack)
{
int size;
if (!stack)
return (0);
size = 0;
while (stack)
{
stack = stack->next;
size++;
}
return (size);
}