-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlst_utils.c
64 lines (56 loc) · 1.55 KB
/
lst_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lst_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrosch <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/04 15:26:11 by lrosch #+# #+# */
/* Updated: 2021/11/24 14:40:52 by lrosch ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *lstnew(int nbr)
{
t_stack *newelement;
newelement = malloc(sizeof(t_stack));
if (newelement == NULL)
return (NULL);
newelement->num = nbr;
newelement->next = NULL;
return (newelement);
}
t_stack *lstlast(t_stack *lst)
{
t_stack *end;
if (!lst)
return (NULL);
while (lst != NULL)
{
if (lst->next == NULL)
{
end = lst;
}
lst = lst->next;
}
return (end);
}
int lstadd_back(t_stack **lst, t_stack *new)
{
t_stack *end;
if (new == NULL)
return (0);
if (!*lst)
*lst = new;
else
{
end = lstlast(*lst);
end->next = new;
}
return (1);
}
void lstadd_front(t_stack **lst, t_stack *new)
{
new->next = *lst;
*lst = new;
}