-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
96 lines (86 loc) · 2.11 KB
/
stack.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dvan-der <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/15 08:15:40 by dvan-der #+# #+# */
/* Updated: 2021/12/15 08:15:46 by dvan-der ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void free_stack(t_stack **lst)
{
t_stack *tmp;
if (!lst)
return ;
while ((*lst))
{
tmp = *lst;
*lst = (*lst)->next;
free(tmp->b_nbr);
free(tmp);
}
*lst = NULL;
}
int create_stack(char *str, t_stack **a, t_stack **new)
{
int d_nbr;
char *b_nbr;
if (!input_correct(str))
return (0);
d_nbr = ft_atoi(str);
b_nbr = ft_atoi_binary_str(d_nbr);
*new = new_node(d_nbr, b_nbr);
if (!(*new))
return (0);
if (!stack_add_back(a, *new))
{
free_stack(new);
return (0);
}
return (1);
}
t_stack *new_node(int dec_nbr, char *bin_nbr)
{
t_stack *new_stack;
new_stack = (t_stack *)malloc(sizeof(t_stack));
if (!new_stack)
return (NULL);
new_stack->d_nbr = dec_nbr;
new_stack->b_nbr = bin_nbr;
new_stack->next = NULL;
return (new_stack);
}
int stack_add_back(t_stack **stack, t_stack *new)
{
t_stack *tmp;
tmp = *stack;
if (!(*stack))
{
*stack = new;
return (1);
}
while (tmp->next != NULL)
{
if (tmp->d_nbr == new->d_nbr)
return (0);
tmp = tmp->next;
}
if (tmp->d_nbr == new->d_nbr)
return (0);
tmp->next = new;
return (1);
}
int stack_add_front(t_stack **stack, t_stack *new)
{
if (!(*stack))
{
*stack = new;
return (1);
}
new->next = *stack;
*stack = new;
return (1);
}