-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedList.c
134 lines (122 loc) · 3.2 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
#include "linkedList.h"
#include "globalDefs.h"
#include "ydThreads.h"
node_t_p list_create(void* data) {
node_t_p node;
if(!(node=malloc(sizeof(node_t)))) {
printf("ERROR cannot allocate memory for node at list_create function\n");
return NULL;
}
node->data=data;
node->next=NULL;
return node;
}
op_status list_insert_after(node_t_p node, void *data) {
node_t_p newnode;
newnode=list_create(data);
newnode->next = node->next;
node->next = newnode;
return OP_SUCCESS;
}
node_t_p list_insert_beginning(node_t_p list, void *data) {
node_t_p newnode;
newnode=list_create(data);
newnode->next = list;
return newnode;
}
op_status list_clear_all_threads(node_t_p list)
{
while(list)
{
node_t_p node = list->next;
free(((mctx_t_p)list->next->data)->uc.uc_stack.ss_sp);
free(((mctx_t_p)list->next->data));
free(list);
list = node;
node = NULL;
}
}
op_status list_remove_thread(node_t_p list, tID id) {
//TODO: fix this..
if (((mctx_t_p) list->data)->id== id)
{
node_t_p node = list->next;
if (node)
{
mctx_t_p data = list->data;
free(data->uc.uc_stack.ss_sp);
free(data);
free(list);
container->container = node;
return OP_SUCCESS;
}
free(((mctx_t_p)list->data)->uc.uc_stack.ss_sp);
list->data == NULL;
free (((mctx_t_p)list->data));
free (list);
return OP_DONE;
}
node_t_p p_list = list;
while(p_list->next && ((mctx_t_p)p_list->next->data)->id != id)
p_list = p_list->next;
if(p_list->next && ((mctx_t_p)p_list->next->data)->id == id) {
node_t_p node = p_list->next;
p_list->next = node->next;
free(((mctx_t_p)node->data)->uc.uc_stack.ss_sp);
free(((mctx_t_p)node->data));
free(node);
node = NULL;
return OP_SUCCESS;
} else return OP_FAIL;
}
int list_foreach(node_t_p node, int(*func)(void*)) {
while(node) {
if(func(node->data)!=0) return -1;
node=node->next;
}
return 0;
}
node_t_p list_find(node_t_p node, int(*func)(void*,void*), void *data) {
//TODO:not working correctly
while(node) {
if(func(node->data, data)>0) return node;
node=node->next;
}
return NULL;
}
node_t_p list_at(node_t_p list, int index) {
ASSERT(list);
node_t_p current = list;
int i= 0;
if (index < 0)
return NULL;
for (i=0; i<index; i++){
if (current != NULL)
current = current->next;
else
return NULL;
}
if (current != NULL)
return current;
else
return NULL;
}
op_status list_add_last (node_t_p list, void* data) {
if (list == NULL)
return OP_FAIL;
return list_add_last_rec (list, data);
}
op_status list_add_last_rec (node_t_p list, void* data) {
if (list->next == NULL){
list->next = list_create(data);
return OP_SUCCESS;
}
return list_add_last_rec (list->next, data);
}
boolean list_is_empty (node_t_p list) {
if (list == NULL)
return true;
if (list-> data == NULL && list->next == NULL)
return true;
return false;
}