-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_order.c
74 lines (66 loc) · 1.8 KB
/
list_order.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_order.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erigolon <erigolon@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/31 10:17:56 by erigolon #+# #+# */
/* Updated: 2023/06/14 12:51:29 by erigolon ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/push_swap.h"
void free_stack(t_stack **stack)
{
t_stack *temp;
if (!stack || !(*stack))
return ;
while (*stack)
{
temp = (*stack)->next;
free(*stack);
*stack = temp;
}
*stack = NULL;
}
int is_ordered(t_stack *stack_a)
{
while (stack_a->next != NULL)
{
if (stack_a->value > stack_a->next->value)
return (0);
stack_a = stack_a->next;
}
return (1);
}
int lstsize_stack(t_stack *stack)
{
int nodes;
nodes = 0;
if (!stack)
return (0);
while (stack)
{
stack = stack->next;
nodes++;
}
return (nodes);
}
void check_order(t_stack **stack_a, t_stack **stack_b)
{
int len_list;
if (is_ordered(*stack_a))
return ;
len_list = lstsize_stack(*stack_a);
assign_index(*stack_a, (len_list + 1));
if (len_list == 1)
return ;
else if (len_list == 2)
do_sa(stack_a);
else if (len_list == 3)
order_three(stack_a);
else
order_big(stack_a, stack_b);
free_stack(stack_a);
free_stack(stack_b);
}