-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_functions.c
107 lines (101 loc) · 2.57 KB
/
main_functions.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
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_functions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/23 18:15:49 by marvin #+# #+# */
/* Updated: 2023/06/23 18:15:49 by marvin ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int check(int *index, int len, int val)
{
int i;
i = 0;
while (i < len)
{
if (index[i] == val)
return (i + 1);
i++;
}
return(i+1);
}
int circular_struct_build(char **argv, t_push_swappa **a,
t_push_swappa ***tail_a)
{
int i;
int *index;
int len;
t_push_swappa *element_i;
t_push_swappa *tmp;
i = 1;
element_i = malloc(sizeof(t_push_swappa));
while (argv[i])
i++;
index = malloc(sizeof(int) * i - 1);
i = 1;
while (argv[i])
{
index[i - 1] = atoi(argv[i]);
i++;
}
len = i - 1;
quicks(index, 0, len - 1);
i=0;
while(i<len)
{
i++;
if(index[i] == index[i-1])
return(-1);
}
i = 1;
element_i->valore = atoi(argv[i]);
element_i->indice = check(index, len, element_i->valore);
element_i->next = NULL;
i++;
*a = element_i;
while (argv[i])
{
tmp = element_i;
element_i = malloc(sizeof(t_push_swappa));
element_i->valore = atoi(argv[i]);
element_i->indice = check(index, len, element_i->valore);
element_i->next = NULL;
element_i->prev = tmp;
tmp->next = element_i;
i++;
}
(*a)->prev = element_i;
**tail_a = element_i;
(**tail_a)->next = *a;
free(index);
return(len);
}
int addfastpathb(t_push_swappa **a, int lentmp)
{
t_push_swappa *next = (*a)->next;
t_push_swappa *prev = (*a)->prev;
int i = 0;
int x = 0;
while(prev && prev->prev->indice != (*a)->indice)
{
if(prev->indice == lentmp)
break;
prev = prev->prev;
x++;
if(x>lentmp)
break;
}
while(next && next->next->indice != (*a)->indice)
{
if(next->indice == lentmp)
break;
next = next->next;
i++;
if(i>lentmp)
break;
}
return(i-x);
}