-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedlist.c
384 lines (317 loc) · 11.1 KB
/
linkedlist.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <errno.h>
#define D 1
#define PRINT(msg) if(D) printf("[line %d] ", __LINE__); printf(msg)
#define LOCK(mutex) errno = pthread_mutex_lock(mutex);\
if(errno){ \
fprintf(stderr, "[%d] ", __LINE__); \
perror("locking error"); \
abort(); \
} \
#define UNLOCK(mutex) errno = pthread_mutex_unlock(mutex); \
if(errno){ \
fprintf(stderr, "[%d] ", __LINE__); \
perror("unlocking error"); \
abort(); \
} \
#define INIT_LOCK(mutex) errno = pthread_mutex_init(mutex, NULL); \
if(errno){ \
fprintf(stderr, "[%d] ", __LINE__); \
perror("init lock error"); \
abort(); \
} \
#define DESTROY_LOCK(mutex) errno = pthread_mutex_destroy(mutex);\
if(errno){ \
fprintf(stderr, "[%d] ", __LINE__); \
perror("destroy lock error"); \
abort(); \
} \
/*
* How to create a new list:
* List * myList = NULL;
*
* Initializing a list, no strings will be stored at this point
* init_list(myList);
*
* inserting a value into a list:
* insert(&myList, key, value);
*
* removing an element from the list:
* delete(&myList, key, &output_location)
*
* printing the list:
* printList(myList);
*
* Deallocating a list:
* destroy_list(&myList);
*/
typedef struct List{
char* key;
char* value;
struct List *next;
pthread_mutex_t lock;
}List;
void init_list(List** list){
List* temp;
temp = malloc(sizeof(List));
temp->key = NULL;
temp->value = NULL;
INIT_LOCK(&temp->lock);
*list = temp;
}
//threads shouldnt be running when this is called, so not going to mutex it
int destroy_list(List **node){
List* currentNode = *node;
List* nextNode;
while (currentNode != NULL){
if(currentNode->next != NULL) nextNode = currentNode->next;
free(currentNode->value);
free(currentNode->key);
DESTROY_LOCK(¤tNode->lock);
free(currentNode);
currentNode = nextNode;
}
*node = NULL;
return 0;
}
int searchList(List **head_ref, char *key, char **outputLocation){
List* temp = *head_ref;
List*head = *head_ref;
if (temp == NULL || key == NULL || temp->key == NULL){
return 1;
}
LOCK(&head->lock);
while (temp != NULL){
if (strcmp(temp->key, key) == 0){
*outputLocation = malloc(1 + strlen(temp->value));
strcpy(*outputLocation, temp->value);
UNLOCK(&head->lock);
return 0;
}
temp = temp->next;
}
UNLOCK(&head->lock);
return 1;
}
void printList(List *list){
List *temp = list;
printf("\n\nkey: value\n");
while (temp != NULL){
printf("%s: %s\n", temp->key, temp->value);
temp = temp->next;
}
}
int insert(List **head_ref, char* key, char* value){
List* temp = *head_ref;
LOCK(&temp->lock);
//check to see if the list is empty
if ((temp->value == NULL) && (temp->key == NULL)){
//reassign the list to an initialized node
temp->key = malloc(strlen(key) + 1);
strcpy(temp->key, key);
temp->value = malloc(strlen(value) + 1);
strcpy(temp->value, value);
temp->next = NULL;
//lock already set up from init
UNLOCK(&temp->lock);
return 0;
}
int searching = 1;
List *last_node = *head_ref;
while (searching){
//to avoid relocking for the first element
if(last_node != *head_ref){
LOCK(&last_node->lock);
}
if (strcmp(last_node->key, key) == 0){
//the key alread exists, so were just gonna reassign its value
char *pointer = realloc(last_node->value, (strlen(value) + 1));
last_node->value = pointer;
strcpy(last_node->value, value);
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
UNLOCK(&temp->lock);
return 0;
} else if(strcmp(last_node->key, key) < 0) {
//last_node->key comes before the new key
//check how the next node compares
if(last_node->next == NULL){
//we hit the end, so just insert a new node and add the string pair
List *new_node = malloc(sizeof(List));
new_node->key = malloc(strlen(key) + 1);
strcpy(new_node->key, key);
new_node->value = malloc(strlen(value) + 1);
strcpy(new_node->value, value);
new_node->next = NULL;
INIT_LOCK(&new_node->lock);
last_node->next = new_node;
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
UNLOCK(&temp->lock);
return 0;
} else if(strcmp(last_node->next->key, key) > 0){
//the next key comes after the new key
//so we need to insert a new node between the current and next node
List *new_node = malloc(sizeof(List));
new_node->key = malloc(strlen(key) + 1);
strcpy(new_node->key, key);
new_node->value = malloc(strlen(value) + 1);
strcpy(new_node->value, value);
INIT_LOCK(&new_node->lock);
new_node->next = last_node->next;
last_node->next = new_node;
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
UNLOCK(&temp->lock);
return 0;
} else {
//reassign the node
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
last_node = last_node->next;
}
} else {
if(last_node == *head_ref){
//then insert a node before the head
List *new_node = malloc(sizeof(List));
new_node->key = malloc(strlen(key) + 1);
strcpy(new_node->key, key);
new_node->value = malloc(strlen(value) + 1);
strcpy(new_node->value, value);
INIT_LOCK(&new_node->lock);
new_node->next = *head_ref;
*head_ref = new_node;
UNLOCK(&last_node->lock);
return 0;
} else if(strcmp(last_node->next->key, key) > 0){
//the next key comes after the new key
//so we need to insert a new node between the current and next node
List *new_node = malloc(sizeof(List));
new_node->key = malloc(strlen(key) + 1);
strcpy(new_node->key, key);
new_node->value = malloc(strlen(value) + 1);
strcpy(new_node->value, value);
INIT_LOCK(&new_node->lock);
new_node->next = last_node->next;
last_node->next = new_node;
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
UNLOCK(&temp->lock);
return 0;
//see if the next node is null
} else if(last_node->next == NULL){
//we hit the end, so just insert a new node and add the string pair
List *new_node = malloc(sizeof(List));
new_node->key = malloc(strlen(key) + 1);
strcpy(new_node->key, key);
new_node->value = malloc(strlen(value) + 1);
strcpy(new_node->value, value);
INIT_LOCK(&new_node->lock);
new_node->next = NULL;
last_node->next = new_node;
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
UNLOCK(&temp->lock);
return 0;
} else {
//reassign the node
if(last_node != *head_ref){
UNLOCK(&last_node->lock);
}
last_node = last_node->next;
}
}
} //end the while loop
UNLOCK(&temp->lock);
return 1;
}
/**
*
* precondition: output location should be uninitialized
*
* return values:
* 0 for successful deletion
* 1 for failure: if the list is empty/null, or we couldn't find the requested key
* -1 for unanticipated failure
*/
int delete(List** list, char* key, char** output){
List* head = *list;
List* current = *list;
if (head == NULL || ((head->value == NULL) && (head->key == NULL))){
//list is empty or uninitialized
return 1;
}
LOCK(&head->lock);
//see if the first node is the one were looking for
if (strcmp(current->key, key) == 0){
*output = malloc(1 + strlen(current->value));
strcpy(*output, current->value);
free(current->key);
current->key = NULL;
free(current->value);
current->value = NULL;
if(current->next != NULL) *list = current->next; //reassign the head of the list to the 2nd node
UNLOCK(¤t->lock);
//DESTROY_LOCK(¤t->lock);
//free(current);
return 0;
}
int searching = 1;
//check the rest of the list
while (searching){
if(current->next == NULL) {
//found the end of the list before finding the key
*output = NULL;
UNLOCK(&head->lock);
return 1;
} else if (strcmp(current->next->key, key) == 0){
//make sure that the pointers dont refer to the same node
if(current != head) LOCK(¤t->lock);
//see if the desired key is between two nodes or is the last node
if(current->next->next == NULL){
//desired key is in the last node
*output = malloc(1 + strlen(current->next->value));
strcpy(*output, current->next->value);
free(current->next->value);
free(current->next->key);
if(current != head){
UNLOCK(¤t->lock);
DESTROY_LOCK(¤t->lock);
}
free(current->next);
current->next = NULL;
UNLOCK(&head->lock);
return 0;
} else {
//key is between two nodes
//create a node pointer for the node to delete
List* targetNode = current->next;
*output = malloc(1 + strlen(current->next->value));
strcpy(*output, current->next->value);
free(current->next->value);
free(current->next->key);
current->next = current->next->next; //redefine the next node
UNLOCK(¤t->lock);
DESTROY_LOCK(¤t->next->lock);
free(targetNode);
UNLOCK(&head->lock);
return 0;
}
} else {
//move onto the next node
current = current->next;
}
}
UNLOCK(&head->lock);
return -1;
}