-
Notifications
You must be signed in to change notification settings - Fork 0
/
4ft_utils.c
executable file
·92 lines (81 loc) · 1.87 KB
/
4ft_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
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 4ft_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: skarunat <skarunat@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/30 11:27:46 by skarunat #+# #+# */
/* Updated: 2023/08/05 19:12:33 by skarunat ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
void ft_free(char **str)
{
int i;
i = 0;
while (str[i])
i++;
while (i >= 0)
free(str[i--]);
free(str);
}
int ft_is_sorted(t_stack **stack)
{
t_stack *head;
head = *stack;
while (head != NULL && head->next != NULL)
{
if (head->value > head->next->value)
return (0);
head = head->next;
}
return (1);
}
int ft_get_space(t_stack **stack, int i)
{
t_stack *head;
int space;
space = 0;
head = *stack;
while (head)
{
if (head->index == i)
break ;
space++;
head = head->next;
}
return (space);
}
void ft_make_top(t_stack **stack, int space)
{
t_stack *head;
int tmp;
if (space == 0)
return ;
head = *stack;
tmp = ft_get_size(head) - space;
if (space <= (ft_get_size(head) / 2))
{
while (space-- > 0)
ft_ra(stack);
}
else
{
while (tmp-- > 0)
ft_rra(stack);
}
}
void ft_free_stack(t_stack **stack)
{
t_stack *head;
t_stack *tmp;
head = *stack;
while (head)
{
tmp = head;
head = head->next;
free(tmp);
}
free(stack);
}