-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_utils.c
80 lines (71 loc) · 1.85 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yenoh <yenoh@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/13 12:36:27 by yenoh #+# #+# */
/* Updated: 2023/11/13 12:36:29 by yenoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int *to_array(t_stack *stack)
{
t_node *tmp;
int *arr;
int i;
tmp = stack->head;
arr = (int *)malloc(sizeof(int) * stack->len);
i = -1;
while (++i < stack->len)
{
arr[i] = tmp->num;
tmp = tmp->next;
}
return (arr);
}
void free_stack(t_stack *stack)
{
t_node *tmp;
t_node *node;
tmp = stack->head;
while (tmp)
{
node = tmp;
tmp = tmp->next;
free(node);
}
free(stack);
}
void print_stack(t_stack *stack)
{
t_node *tmp;
if (stack->len == 0)
return ;
tmp = stack->head;
ft_printf("head: %d, tail: %d\n", stack->head->num, stack->tail->num);
ft_printf("stack: ");
while (tmp)
{
ft_printf(" %d", tmp->num);
tmp = tmp->next;
}
ft_printf("\nreverse stack:");
tmp = stack->tail;
while (tmp)
{
ft_printf(" %d", tmp->num);
tmp = tmp->prev;
}
ft_printf("\n\n");
}
void print_arr(int *arr, t_stack *stack)
{
int i;
ft_printf("arr:");
i = -1;
while (++i < stack->len)
ft_printf(" %d", arr[i]);
ft_printf("\n\n");
}