-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_utils.c
90 lines (80 loc) · 1.33 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
88
89
90
#include "push_swap.h"
t_stack *new_stack(int32_t value, t_two_stacks **stacks)
{
t_stack *new;
new = (t_stack *)malloc(sizeof(t_stack));
if (!new)
error_handle(stacks);
new->next = NULL;
new->value = value;
new->length = 1;
new->index = 0;
new->chunk = 0;
new->chunk_b = 0;
return (new);
}
t_stack *push(t_stack *stack, int32_t value, int32_t index, int32_t chunk)
{
t_stack *new;
t_stack *tmp;
if (!stack)
{
stack = new_stack(value, NULL);
stack->index = index;
stack->chunk = chunk;
}
else
{
new = new_stack(value, NULL);
new->length = stack->length + 1;
new->index = index;
new->chunk = chunk;
tmp = stack;
stack = new;
stack->next = tmp;
}
return (stack);
}
int32_t pop(t_stack **stack)
{
int32_t tmp;
t_stack *out;
out = *stack;
tmp = out->value;
*stack = out->next;
free(out);
return (tmp);
}
t_two_stacks *stacks_init(t_stack *a, t_stack *b)
{
t_two_stacks *new;
new = (t_two_stacks *)malloc(sizeof(t_two_stacks));
if (!new)
return (NULL);
new->a = a;
new->b = b;
new->min = 0;
new->max = 0;
new->len_b = 0;
return (new);
}
int32_t is_no_repited(t_stack *a)
{
t_stack *tmp;
t_stack *save;
tmp = a;
save = a;
while (a->next)
{
tmp = a->next;
while (tmp)
{
if (tmp->value == a->value)
return (1);
tmp = tmp->next;
}
a = a->next;
}
a = save;
return (0);
}