-
Notifications
You must be signed in to change notification settings - Fork 0
/
push_swap_utils.c
95 lines (87 loc) · 2.36 KB
/
push_swap_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
93
94
95
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* push_swap_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dnishsha <dnishsha@student.42berlin.d +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/06 09:24:22 by dnishsha #+# #+# */
/* Updated: 2023/07/06 09:24:27 by dnishsha ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// Convert a string into a long integer.
// In case of an error "2147483648" number is returned.
long int ft_atol(const char *str)
{
long int num;
int sign;
sign = 1;
num = 0;
if (!*str)
return (2147483648);
if (*str && *str == '-' && (*(str + 1) <= '9' && *(str + 1) >= '0'))
{
sign = -1;
str ++;
}
while (*str)
{
if (*str > '9' || *str < '0')
return (2147483648);
num *= 10;
num += *str - '0';
str ++;
}
return (num * (sign));
}
// Check for duplicate numbers. Return 0 if there are no duplicate values.
int check_duplicate(long int num, t_stack_node *a)
{
while (a)
{
if (a->value == num)
return (1);
a = a->next ;
}
return (0);
}
void update_target_nodes(t_stack_node **b, t_stack_node *a)
{
size_t target_val;
t_stack_node *curr_node;
if (!(*b))
return ;
curr_node = *b;
while (curr_node)
{
target_val = find_target_node(a, curr_node->value);
curr_node->target_node = target_val;
curr_node = curr_node->next;
}
cal_push_price(a, *b);
}
// Reset rotation Details
void reset_seq(t_stack_node *stack)
{
if (stack)
{
stack->ra = 0;
stack->rb = 0;
stack->rr = 0;
stack->rra = 0;
stack->rrb = 0;
stack->rrr = 0;
}
}
// Reset rotation details, push_price, target_node
void reset_node_data(t_stack_node *stack)
{
while (stack)
{
stack->push_price = 0;
stack->target_node = 0;
reset_seq(stack);
stack = stack->next;
}
}