-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities_3.c
101 lines (90 loc) · 1.99 KB
/
utilities_3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utilities_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: natamazy <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 11:50:49 by natamazy #+# #+# */
/* Updated: 2024/02/26 18:10:59 by natamazy ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int is_stack_sorted(t_stack *sf)
{
t_list *tmp;
if (!sf)
return (0);
tmp = sf->start;
while (tmp->next)
{
if (tmp->nbr < tmp->next->nbr)
tmp = tmp->next;
else
break ;
}
if (tmp == sf->finish)
return (1);
return (0);
}
int length_of_stack(t_stack *sf)
{
t_list *tmp;
int length;
if (!sf)
return (INT_MIN);
tmp = sf->start;
length = 0;
while (tmp)
{
length += 1;
tmp = tmp->next;
}
return (length);
}
t_list *highest_node(t_stack *sf)
{
t_list *tmp;
t_list *highest;
if (!sf)
return (NULL);
tmp = sf->start;
highest = sf->start;
while (tmp)
{
if (tmp->index > highest->index)
highest = tmp;
tmp = tmp->next;
}
return (highest);
}
int position_of_node(t_stack *sf, t_list *node)
{
t_list *tmp;
int i;
tmp = sf->start;
i = 0;
while (tmp)
{
if (tmp == node)
return (i);
i++;
tmp = tmp->next;
}
return (i);
}
int position_of_node_by_index(t_stack *sf, int i)
{
t_list *tmp;
int position;
tmp = sf->start;
position = 0;
while (tmp)
{
if (tmp->index == i)
return (position);
position++;
tmp = tmp->next;
}
return (INT_MIN);
}