-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
89 lines (78 loc) · 1.92 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kristori <kristori@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 13:16:59 by kristori #+# #+# */
/* Updated: 2023/01/12 10:23:39 by kristori ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_get_index_val(t_stack *head, int index)
{
t_stack *current;
int k;
k = 0;
current = head;
while (current != NULL)
{
if (k == index)
return (current->data);
k++;
current = current->next;
}
return (0);
}
int ft_stack_len(t_stack *head)
{
t_stack *current;
int k;
k = 0;
current = head;
while (current != NULL)
{
k++;
current = current->next;
}
return (k);
}
int ft_get_last_item(t_stack *head)
{
t_stack *current;
current = head;
if (current == NULL)
return (0);
while (current->next != NULL)
current = current->next;
return (current->data);
}
void ft_remove_last_node(t_stack **head)
{
t_stack *tmp;
t_stack *last_node;
if (*head != NULL)
{
if ((*head)->next == NULL)
*head = NULL;
else
{
tmp = *head;
while (tmp->next->next != NULL)
tmp = tmp->next;
last_node = tmp->next;
tmp->next = NULL;
free(last_node);
}
}
}
void ft_remove_first_node(t_stack **head)
{
t_stack *tmp;
if (head == NULL)
return ;
tmp = *head;
*head = (*head)->next;
free(tmp);
}