-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexing.c
83 lines (75 loc) · 949 Bytes
/
indexing.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
#include "push_swap.h"
int *str_to_arr(t_stack *a)
{
int i;
int *nums;
int len;
i = 0;
len = ft_lstsize(a);
nums = malloc(sizeof(int) * len);
while (a)
{
nums[i] = a->data;
i++;
a = a->next;
}
return (nums);
}
int *bubble_sort(t_stack *a)
{
int *nums;
int i;
int j;
int len;
nums = str_to_arr(a);
len = ft_lstsize(a);
i = 0;
while (i < len)
{
j = i + 1;
while (j < len)
{
if (nums[i] > nums[j])
ft_swap(&nums[i], &nums[j]);
j++;
}
i++;
}
return (nums);
}
void indexing(t_stack *a)
{
int *nums;
int i;
int len;
nums = bubble_sort(a);
len = ft_lstsize(a);
while (a)
{
i = 0;
while (i < len)
{
if (a->data == nums[i])
a->pos = i;
i++;
}
a = a->next;
}
}
int place_in_stack(t_stack **a, int data)
{
int i;
int index;
t_stack *tmp;
tmp = *a;
index = 0;
i = 0;
while (tmp)
{
if (tmp->data == data)
index = i;
i++;
tmp = tmp->next;
}
return (index);
}