forked from Sistemas-Operativos-Matcom/proyecto-concurrencia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
200 lines (190 loc) · 4.36 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include "list.h"
#include "stdio.h"
#include "stdlib.h"
// Init list structure
int init_list(int_ll_t *list)
{
list->root = NULL;
list->size = 0;
list->last = NULL;
pthread_mutex_init(&list->lock, NULL);
return 0;
}
// Free list structure
int free_list(int_ll_t *list)
{
pthread_mutex_lock(&list->lock);
Node *u = list->root;
while (u != NULL)
{
Node *v = u;
u = u->next;
free(v);
}
pthread_mutex_unlock(&list->lock);
free(list);
return 0;
}
// Get list size
int size_list(int_ll_t *list)
{
pthread_mutex_lock(&list->lock);
int size = list->size;
pthread_mutex_unlock(&list->lock);
return size;
}
// Get element at index
int index_list(int_ll_t *list, int index, int *out_value)
{
pthread_mutex_lock(&list->lock);
if(index <= 0)
{
*out_value = list->root->value;
pthread_mutex_unlock(&list->lock);
return 0;
}
if(index >= list->size - 1)
{
*out_value = list->last->value;
pthread_mutex_unlock(&list->lock);
return 0;
}
Node *u = list->root;
while (u->index == index + 1)
{
if(u->index == index)
{
*out_value = u->value;
pthread_mutex_unlock(&list->lock);
return 0;
}
u = u->next;
}
pthread_mutex_unlock(&list->lock);
return 0;
}
// Insert element at index
int insert_list(int_ll_t *list, int index, int value)
{
pthread_mutex_lock(&list->lock);
//Node *node = newNode(index, value);
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL)
{
perror("malloc");
pthread_mutex_unlock(&list->lock);
return 1;
}
node->value = value;
node->index = index;
node->next = NULL;
if(list->size == 0)
{
node->index = 0;
list->root = node;
list->last = node;
list->size = 1;
pthread_mutex_unlock(&list->lock);
return 0;
}
if(index <= 0)
{
node->next = list->root;
node->index = 0;
list->size += 1;
list->root = node;
Node *u = node->next;
while (u != NULL)
{
u->index += 1;
u = u->next;
}
pthread_mutex_unlock(&list->lock);
return 0;
}
if(index >= list->size - 1)
{
node->index = list->size;
list->last->next = node;
list->last = node;
list->size += 1;
pthread_mutex_unlock(&list->lock);
return 0;
}
Node *u = list->root;
int founded = 0;
while (u != NULL)
{
if(founded) u->index += 1;
if(u->index == node->index - 1)
{
founded = 1;
Node *v = u->next;
u->next = node;
node->next = v;
u = node->next;//saltarse "node" en el ciclo para no cambiar su indice
continue;
}
u = u->next;
}
pthread_mutex_unlock(&list->lock);
return 0;
}
// Remove element at index
int remove_list(int_ll_t *list, int index, int *out_value)
{
pthread_mutex_lock(&list->lock);
if(list->size == 0) return 0;
if(index <= 0)
{
Node *node = list->root;
*out_value = list->root->value;
list->root = list->root->next;
list->size -= 1;
free(node);
Node *u = list->root;
while (u != NULL)
{
u->index -= 1;
u = u->next;
}
pthread_mutex_unlock(&list->lock);
return 0;
}
if(index >= list->size - 1)
{
*out_value = list->last->value;
list->size -= 1;
Node *node = list->last;
Node *u = list->root;
while (u != NULL)
{
if(u->index == list->size - 1)
{
u->next = NULL;
list->last = u;
}
u = u->next;
}
free(node);
pthread_mutex_unlock(&list->lock);
return 0;
}
Node *u = list->root;
int founded = 0;
while (u != NULL)
{
if(founded) u->index -= 1;
if(u->index == index - 1)
{
founded = 1;
Node *node = u->next;
u->next = node->next;
free(node);
list->size -= 1;
}
u = u->next;
}
pthread_mutex_unlock(&list->lock);
return 0;
}