-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_utils.c
87 lines (78 loc) · 1.78 KB
/
stack_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joandre- <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/20 08:46:24 by joandre- #+# #+# */
/* Updated: 2024/04/20 01:37:40 by joandre- ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *create_node(int n)
{
t_stack *new;
new = malloc(sizeof(t_stack));
if (new)
{
new->prev = NULL;
new->next = NULL;
new->numb = n;
}
return (new);
}
t_stack *last_node(t_stack *a)
{
if (a == NULL)
return (NULL);
while (a->next)
a = a->next;
return (a);
}
unsigned int stack_size(t_stack *a)
{
unsigned int i;
i = 0;
while (a)
{
a = a->next;
++i;
}
return (i);
}
void free_stack(t_stack *a)
{
t_stack *b;
while (a)
{
b = a;
a = a->next;
free(b);
b = NULL;
}
}
void create_stack(t_stack **a, char **n, unsigned int i)
{
t_stack *new;
while (n[i])
{
while (need_parse(n[i]))
parse_2stack(a, n[i++]);
if (n[i])
{
if (stack_size(*a))
new = create_node(ft_atoi(n[i++]));
else
{
*a = create_node(ft_atoi(n[i++]));
continue ;
}
if (new)
{
new->prev = last_node(*a);
last_node(*a)->next = new;
}
}
}
}