-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgl_del_element.c
60 lines (52 loc) · 1.11 KB
/
gl_del_element.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
/*
** EPITECH PROJECT, 2021
** CPP_D02A
** File description:
** del_element.c
*/
#include <unistd.h>
#include <stdbool.h>
#include <stdlib.h>
#include "generic_list.h"
bool list_del_elem_at_front(list_t *front_ptr)
{
list_t tmp;
if (*front_ptr == NULL)
return (false);
tmp = (*front_ptr)->next;
free(*front_ptr);
*front_ptr = tmp;
return (true);
}
bool list_del_elem_at_back(list_t *front_ptr)
{
list_t *prev = front_ptr;
list_t tmp;
if (*front_ptr == NULL)
return (false);
tmp = (*front_ptr)->next;
while (tmp != NULL) {
prev = &(*prev)->next;
tmp = tmp->next;
}
free(*prev);
*prev = NULL;
return (true);
}
bool list_del_elem_at_position(list_t *front_ptr, unsigned int position)
{
list_t *prev = front_ptr;
list_t tmp;
if (*front_ptr == NULL)
return (false);
tmp = (*front_ptr)->next;
for (unsigned ctr = 0; ctr < position; ctr += 1) {
if (tmp == NULL)
return (false);
prev = &(*prev)->next;
tmp = tmp->next;
}
free(*prev);
*prev = tmp;
return (true);
}