-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack_utils.c
86 lines (77 loc) · 1.89 KB
/
stack_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lburkins <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 11:41:28 by lburkins #+# #+# */
/* Updated: 2024/03/04 14:15:19 by lburkins ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_node *find_last_node(t_node *lst)
{
t_node *ptr;
if (lst == NULL)
return (NULL);
ptr = lst;
while (ptr->next != NULL)
ptr = ptr->next;
return (ptr);
}
int count_nodes(t_node *lst)
{
int count;
t_node *ptr;
if (!lst)
return (0);
count = 0;
ptr = lst;
while (ptr)
{
ptr = ptr->next;
count++;
}
return (count);
}
t_node *find_max(t_node *stack)
{
t_node *max_node;
t_node *current_node;
int max;
if (!stack)
return (NULL);
current_node = stack;
max = INT_MIN;
while (current_node)
{
if (current_node->num > max)
{
max = current_node->num;
max_node = current_node;
}
current_node = current_node->next;
}
return (max_node);
}
t_node *find_min(t_node *stack)
{
t_node *min_node;
t_node *current_node;
int min;
if (!stack)
return (NULL);
current_node = stack;
min = INT_MAX;
while (current_node)
{
if (current_node->num < min)
{
min = current_node->num;
min_node = current_node;
}
current_node = current_node->next;
}
return (min_node);
}