-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstack_mod.c
83 lines (72 loc) · 1.86 KB
/
stack_mod.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_mod.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lde-ross < [email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/16 15:29:01 by lde-ross #+# #+# */
/* Updated: 2023/01/27 13:51:03 by lde-ross ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *new_stack(int value, int index)
{
t_stack *stack;
stack = malloc(sizeof(t_stack));
if (!stack)
return (NULL);
stack->value = value;
stack->index = index;
stack->next = NULL;
return (stack);
}
void stack_update_index(t_stack **stack)
{
t_stack *temp;
int i;
i = 0;
temp = *stack;
while (temp)
{
temp->index = i;
temp = temp->next;
i++;
}
}
void stack_add_back(t_stack *stack, t_stack *new_node)
{
t_stack *last;
last = stack_get_last(stack);
last->next = new_node;
}
void clear_stack(t_stack **stack)
{
t_stack *tmp;
if (stack == NULL)
return ;
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
*stack = NULL;
}
t_stack *init_stack(int length, char *argv[])
{
t_stack *stack;
int i;
int number;
i = 1;
while (i <= length)
{
number = map_number(length, argv, i);
if (i == 1)
stack = new_stack(number, i - 1);
else
stack_add_back(stack, new_stack(number, i - 1));
i++;
}
return (stack);
}