-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_tools.c
83 lines (72 loc) · 1.9 KB
/
stack_tools.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_tools.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ytaya <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/30 23:31:20 by ytaya #+# #+# */
/* Updated: 2022/02/01 15:14:07 by ytaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_type *ft_lstnew(int number)
{
t_type *newlst;
newlst = (t_type *) malloc(sizeof(t_type));
if (!newlst)
return (0);
newlst->next = NULL;
newlst->num = number;
return (newlst);
}
t_type *ft_lstlast(t_type *lst)
{
t_type *ptrlst;
if (lst == NULL)
return (lst);
ptrlst = lst;
while (ptrlst->next)
ptrlst = ptrlst->next;
return (ptrlst);
}
void ft_lstadd_back(t_type **lst, t_type *new)
{
t_type *ptrlst;
if (!(*lst))
{
*lst = new;
return ;
}
ptrlst = ft_lstlast(*lst);
ptrlst->next = new;
}
void ft_fill_stack(char **argv, t_type **stack)
{
int i;
t_type *head;
i = 0;
if (argv[i] && ft_isdigit(argv[i]))
head = ft_lstnew(ft_atoi(argv[i++]));
else
ft_error("\033[0;31mError\n");
while (argv[i])
{
if (ft_isdigit(argv[i]) && !ft_dupl(ft_atoi(argv[i]), head))
ft_lstadd_back(&head, ft_lstnew(ft_atoi(argv[i++])));
else
ft_error("\033[0;31mError\n");
}
*stack = head;
}
int ft_lstsize(t_type *lst)
{
int i;
i = 0;
while (lst)
{
i++;
lst = lst->next;
}
return (i);
}