This repository was archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexes.c
63 lines (58 loc) · 1.76 KB
/
indexes.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* indexes.c :+: :+: */
/* +:+ */
/* By: fbes <fbes@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/10/27 19:17:33 by fbes #+# #+# */
/* Updated: 2021/11/01 21:47:47 by fbes ######## odam.nl */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include "push_swap.h"
/**
* Adds two indexes to a stack: to the smallest and the biggest numbers
* @param i The current iteration in the loop of indexing
* @param *s The stack to index
*/
static void index_next(const int i, t_stack *s)
{
t_link *current;
t_link *smallest;
t_link *biggest;
smallest = NULL;
biggest = NULL;
current = s->top;
while (current)
{
if (current->id > -1)
{
current = current->next;
continue ;
}
if (!smallest || current->num < smallest->num)
smallest = current;
if (!biggest || current->num > biggest->num)
biggest = current;
current = current->next;
}
smallest->id = i;
biggest->id = s->size - i - 1;
}
/**
* Adds indexes to a stack
* @param *s The stack to add indexes to
*/
void index_stack(t_stack *s)
{
int i;
int to;
i = 0;
to = s->size * 0.5 + s->size % 2;
while (i < to)
{
index_next(i, s);
i++;
}
}