-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_utils.c
More file actions
95 lines (83 loc) · 1.97 KB
/
stack_utils.c
File metadata and controls
95 lines (83 loc) · 1.97 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sarfreit <sarfreit@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/01/23 00:03:43 by sarfreit #+# #+# */
/* Updated: 2026/01/23 00:03:43 by sarfreit ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *last_node(t_stack *stack)
{
t_stack *node;
if (!stack)
return (NULL);
node = stack;
while (node->next)
{
node = node->next;
}
return (node);
}
// Add nodes to the back or front of stack a on parse_args
void add_node(t_stack **lst, t_stack *new, char *type)
{
t_stack *last;
if (!lst || !new || !type)
return ;
new->next = NULL;
if (type[0] == 'b')
{
if (*lst == NULL)
*lst = new;
else
{
last = last_node(*lst);
last->next = new;
}
}
else if (type[0] == 'f')
{
new->next = *lst;
*lst = new;
}
}
t_stack *new_node(int value)
{
t_stack *node;
node = (t_stack *)malloc(sizeof(t_stack));
if (!node)
return (NULL);
node->value = value;
node->next = NULL;
return (node);
}
// Calculate the size of each stack
int stack_size(t_stack *stack)
{
int counter;
t_stack *node;
counter = 0;
node = stack;
while (node)
{
node = node->next;
counter++;
}
return (counter);
}
void free_stack(t_stack **stack)
{
t_stack *tmp;
if (!stack || !*stack)
return ;
while (*stack)
{
tmp = (*stack)->next;
free(*stack);
*stack = tmp;
}
}