-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_pos.c
More file actions
112 lines (99 loc) · 2.29 KB
/
stack_pos.c
File metadata and controls
112 lines (99 loc) · 2.29 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_pos.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sarfreit <sarfreit@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/23 00:14:54 by sarfreit #+# #+# */
/* Updated: 2026/01/23 00:14:54 by sarfreit ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *stack_min_node(t_stack *stack)
{
t_stack *min_node;
t_stack *node;
if (!stack)
return (NULL);
node = stack;
min_node = node;
while (node)
{
if (node->value < min_node->value)
min_node = node;
node = node->next;
}
return (min_node);
}
t_stack *stack_max_node(t_stack *stack)
{
t_stack *max_node;
t_stack *node;
if (!stack)
return (NULL);
node = stack;
max_node = node;
while (node)
{
if (node->value > max_node->value)
max_node = node;
node = node->next;
}
return (max_node);
}
// For example, find index of the max/min number
int position_of_node(t_stack *stack, t_stack *target)
{
int index;
t_stack *node;
index = 0;
if (!stack || !target)
return (-1);
node = stack;
while (node)
{
if (node == target)
return (index);
index++;
node = node->next;
}
return (-1);
}
// For each number to have an index, sorted by raking
// "How many values are smaller than runner?"
void assign_index(t_stack *stack)
{
t_stack *runner;
t_stack *node;
int rank;
if (!stack)
return ;
node = stack;
while (node)
{
rank = 0;
runner = stack;
while (runner)
{
if (runner->value < node->value)
rank++;
runner = runner->next;
}
node->index = rank;
node = node->next;
}
}
int position_by_index(t_stack *s, int target_idx)
{
int pos;
pos = 0;
while (s)
{
if (s->index == target_idx)
return (pos);
s = s->next;
pos++;
}
return (-1);
}