-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_utils_1.c
92 lines (82 loc) · 1.94 KB
/
list_utils_1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* list_utils_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvonsovs <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 16:37:13 by fvonsovs #+# #+# */
/* Updated: 2023/04/17 18:05:35 by fvonsovs ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// list functions
t_stack *new_stack(int val)
{
t_stack *stack;
stack = (t_stack *)malloc(sizeof(t_stack));
if (!stack)
return (NULL);
stack->num = val;
stack->index = -1;
stack->next = NULL;
return (stack);
}
// returns last element of stack
t_stack *stack_last(t_stack *stack)
{
t_stack *tmp;
if (!stack)
return (NULL);
tmp = stack;
while (tmp->next)
{
tmp = tmp->next;
if (tmp->next == NULL)
return (tmp);
}
return (tmp);
}
void stackadd_back(t_stack **stack, t_stack *new)
{
t_stack *tmp;
if (*stack)
{
tmp = stack_last(*stack);
tmp->next = new;
new->next = NULL;
}
else
{
*stack = new;
(*stack)->next = NULL;
}
}
void stackadd_front(t_stack **lst, t_stack *new)
{
if (lst)
{
if (*lst)
new->next = *lst;
*lst = new;
}
}
// updates index according to min value
void update_index(t_stack *stack)
{
t_stack *temp;
int index;
temp = stack;
while (temp)
{
temp->index = -1;
temp = temp->next;
}
index = 0;
temp = get_min(&stack);
while (temp)
{
temp->index = index++;
temp = get_min(&stack);
}
}