-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_utils.c
56 lines (50 loc) · 1.53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amacarul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/10 17:09:02 by amacarul #+# #+# */
/* Updated: 2024/10/17 17:00:52 by amacarul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libpush_swap.h"
//More utils for manage stacks
//Count stack nodes
int stack_len(t_stack *stack)
{
t_node *current;
int counter;
current = stack->top;
counter = 0;
while (current != NULL)
{
current = current->next;
counter ++;
}
return (counter);
}
//Last node
t_node *last_node(t_stack *stack)
{
t_node *last;
last = stack->top;
while ((last->next) != NULL)
last = last->next;
return (last);
}
//Copy a stack
t_stack *copy_stack(t_stack *stack)
{
t_stack *copy;
t_node *current;
copy = init_stack();
current = stack->top;
while (current != NULL)
{
push_bottom(copy, current->val);
current = current->next;
}
return (copy);
}