-
Notifications
You must be signed in to change notification settings - Fork 0
/
linked_list.c
74 lines (66 loc) · 1.52 KB
/
linked_list.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
/*
** EPITECH PROJECT, 2021
** B-PSU-400-RUN-4-1-malloc-paul.brancieq
** File description:
** linkedList
*/
#include "include/my_malloc.h"
#include <stdlib.h>
my_malloc_t *create_elem(void *ptr, int block_size, state_t state)
{
my_malloc_t *elem = ptr;
if (block_size < sizeof(my_malloc_t) || ptr == NULL)
return NULL;
elem->b_offset = block_size - sizeof(my_malloc_t);
elem->memory = (void *)ptr + sizeof(my_malloc_t);
elem->next = NULL;
elem->prev = NULL;
elem->state = state;
return (elem);
}
int suppr_elem(my_malloc_t *list)
{
if (list == NULL)
return 1;
if (list->prev != NULL)
{
list->prev->next = list->next;
}
if (list->next != NULL)
{
list->next->prev = list->prev;
}
return 0;
}
int insert_elem(my_malloc_t *new_elem, my_malloc_t *next)
{
if (next == NULL)
return (0);
else if (new_elem == NULL)
return 1;
next->prev = new_elem;
next->next = new_elem->next;
if (next->next != NULL)
next->next->prev = next;
new_elem->next = next;
return 0;
}
my_malloc_t *get_last(my_malloc_t *list)
{
if (list == NULL)
return NULL;
while (list->next != NULL)
list = list->next;
return list;
}
my_malloc_t *add_elem_at_end(my_malloc_t *elem)
{
my_malloc_t *end_list = get_last(memory_list);
if (elem == NULL)
return memory_list;
if (end_list == NULL)
return memory_list = elem;
elem->prev = end_list;
end_list->next = elem;
return elem;
}