-
Notifications
You must be signed in to change notification settings - Fork 0
/
lists.c
128 lines (118 loc) · 2.69 KB
/
lists.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */ /* */
/* ::: :::::::: */
/* lists.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hde-vos <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/25 14:39:36 by hde-vos #+# #+# */
/* Updated: 2019/09/03 12:44:05 by hde-vos ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
t_stack *createnode(int n)
{
t_stack *new;
new = (t_stack *)malloc(sizeof(t_stack));
if (new)
{
new->value = n;
new->weight = 0;
new->next = NULL;
new->prev = NULL;
}
return (new);
}
void add_head(t_stack **stack, t_stack *node)
{
t_stack *temp;
temp = *stack;
if (temp)
{
node->prev = NULL;
node->next = *stack;
*stack = node;
}
else
{
node->next = NULL;
node->prev = NULL;
*stack = node;
}
}
void add_tail(t_stack *new, t_stack **stack)
{
t_stack *temp;
temp = *stack;
if (temp)
{
while (temp->next)
temp = temp->next;
temp->next = new;
new->prev = temp;
}
}
void print_stack(t_stack **stack)
{
t_stack *new;
t_stack *prev;
new = *stack;
prev = NULL;
if (new && stack)
{
ft_putstr("--- Stack ---\n");
while (new)
{
if (new == prev)
{
ft_putstr("Cycle in linked list\n");
break ;
}
ft_putnbr(new->value);
ft_putstr("\t[");
ft_putnbr(new->weight);
ft_putstr("]\t{");
ft_putnbr(new->move_count);
ft_putstr("}\t(");
ft_putnbr(new->best_pos_weight);
ft_putstr(")\t[");
ft_putnbr(new->end);
ft_putstr("]\t{");
ft_putnbr(new->total_move_count);
ft_putstr("}\n");
prev = new;
new = new->next;
}
ft_putstr("-------------\n");
}
}
t_stack *stackfill(int n, char **args)
{
t_stack *stack;
t_stack *new;
int i;
char *value;
i = 0;
new = NULL;
value = ft_itoa(ft_atoi(args[0]));
if (ft_strequ(value, args[0]) != 0 && is_duplicate(args) != 0)
{
free(value);
stack = createnode(ft_atoi(args[i]));
while (++i < n)
{
value = ft_itoa(ft_atoi(args[i]));
if (ft_strequ(value, args[i]) == 0)
{
free(value);
free_stack(&stack);
return (NULL);
}
free(value);
new = createnode(ft_atoi(args[i]));
add_tail(new, &stack);
}
return (stack);
}
free(value);
return (NULL);
}