forked from Sistemas-Operativos-Matcom/proyecto-concurrencia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
116 lines (90 loc) · 1.81 KB
/
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
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
#include "list.h"
#include "pthread.h"
#include "stdio.h"
#include "stdlib.h"
int size = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
// Add node
void add_node(int_ll_t *out_node, int_ll_t *next, int value)
{
out_node = (int_ll_t *)malloc(sizeof(int_ll_t));
out_node->value = value;
out_node->next = next;
pthread_mutex_init(&out_node->mutex, NULL);
}
// Init list structure
int init_list(int_ll_t *list)
{
add_node(list, NULL, 1);
return 0;
}
// Free list structure
int free_list(int_ll_t *list)
{
while(list->next)
{
free_list(list->next);
}
free(list);
return 0;
}
// Get list size
int size_list(int_ll_t *list)
{
return size;
}
// Get element at index
int index_list(int_ll_t *list, int index, int *out_value)
{
if(index = -1 || index >= size) return 1;
int_ll_t *node = list;
while (index != 0)
{
node = node->next;
index--;
}
*out_value = node->value;
return 0;
}
// Insert element at index
int insert_list(int_ll_t *list, int index, int value)
{
if(index = -1 || index >= size) return 1;
int_ll_t *node = list;
int_ll_t *new;
while(index != 0)
{
node = node->next;
index--;
}
add_node(new, node->next, value);
node->next = new;
size += 1;
return 0;
}
// Remove element at index
int remove_list(int_ll_t *list, int index, int *out_value)
{
if(index = -1 || index >= size)
{
*out_value = 1;
return 1;
}
int_ll_t *node = list;
if(index == 0)
{
list = list->next;
free(node);
return 0;
}
while(index != 1)
{
node = list->next;
index--;
}
int_ll_t *old = node->next;
node->next = node->next->next;
free(old);
size -= 1;
return 0;
}