-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_utils.c
More file actions
90 lines (81 loc) · 1.91 KB
/
stack_utils.c
File metadata and controls
90 lines (81 loc) · 1.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joamonte <joamonte@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 12:04:35 by joamonte #+# #+# */
/* Updated: 2024/03/27 10:58:52 by joamonte ### ########.fr */
/* */
/* ************************************************************************** */
#include "./push_swap.h"
int stack_len(t_stack_node *stack)
{
int len;
if (!stack)
return (0);
len = 0;
while (stack)
{
stack = stack->next;
len++;
}
return (len);
}
t_stack_node *find_last(t_stack_node *stack)
{
if (!stack)
return (NULL);
while (stack->next)
stack = stack->next;
return (stack);
}
bool stack_sorted(t_stack_node *stack)
{
if (!stack)
return (1);
while (stack->next)
{
if (stack->nbr > stack->next->nbr)
return (false);
stack = stack->next;
}
return (true);
}
t_stack_node *find_min(t_stack_node *stack)
{
long min;
t_stack_node *min_node;
if (!stack)
return (NULL);
min = LONG_MAX;
while (stack)
{
if (stack->nbr < min)
{
min = stack->nbr;
min_node = stack;
}
stack = stack->next;
}
return (min_node);
}
t_stack_node *find_max(t_stack_node *stack)
{
long max;
t_stack_node *max_node;
if (!stack)
return (NULL);
max = LONG_MIN;
while (stack)
{
if (stack->nbr > max)
{
max = stack->nbr;
max_node = stack;
}
stack = stack->next;
}
return (max_node);
}