-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.c
More file actions
49 lines (44 loc) · 1010 Bytes
/
linked_list.c
File metadata and controls
49 lines (44 loc) · 1010 Bytes
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
#include <stdlib.h>
#include <stdio.h>
#include "linked_list.h"
void list_init(struct linked_list_head *list) {
list->head=NULL;
list->sync = rw_init();
}
struct linked_list* list_remove(struct linked_list_head *list, int val) {
struct linked_list **p, *ret=NULL;
begin_write(list->sync);
p=&list->head;
while (*p) {
if ((*p)->nb==val) {
ret=*p;
*p=(*p)->next;
break;
}
p=&(*p)->next;
}
end_write(list->sync);
return ret;
}
void list_insert(struct linked_list_head *list, int val){
begin_write(list->sync);
struct linked_list *new_cell = malloc(sizeof(struct linked_list));
new_cell->next = list->head;
new_cell->nb = val;
list->head = new_cell;
end_write(list->sync);
}
int list_exists(struct linked_list_head *list, int val) {
struct linked_list *p;
begin_read(list->sync);
p=list->head;
while (p) {
if (p->nb==val) {
end_read(list->sync);
return 1;
}
p=p->next;
}
end_read(list->sync);
return 0;
}