-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.c
146 lines (108 loc) · 2.51 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
pool_t* create_pool(){
pool_t *new =(pool_t *) malloc (sizeof(pool_t));
assert(new);
new->head=NULL;
new->tail=NULL;
return new;
}
list_t * create_list(){
list_t *new =(list_t *) malloc (sizeof(list_t));
assert(new);
new->args=NULL;
new->next=NULL;
return new;
}
list_t* add_pool_head(pool_t *pool, void *element){
list_t *new = (list_t *) malloc(sizeof(list_t));
assert(new);
if(!pool->head){
pool->head = new;
pool->tail=new;
new->next = NULL;
new->args = element;
}
else{
new->args = element;
new->next = pool->head;
pool->head=new;
}
return new;
}
list_t* add_pool_tail(pool_t *pool, void *element){
list_t *new = (list_t *) malloc(sizeof(list_t));
assert(new);
if(!pool->tail){
pool->head = new;
pool->tail=new;
new->next = NULL;
new->args = element;
}else{
new->args = element;
pool->tail->next = new;
pool->tail = new;
}
return new;
}
void* remove_element(pool_t *pool, list_t *node, list_t *prev){
void *ptr;
if (prev == NULL && node != pool->head)
assert(0);
if(node == pool->head ){
pool->head = node->next;
if (node == pool->tail)
pool->head=pool->tail = NULL;
node->next = NULL;
ptr = node->args;
}
else if(node == pool->tail){
pool->tail = prev ;
prev->next = NULL;
ptr = node->args;
}
else{
prev->next = node->next;
node->next = NULL;
ptr = node->args;
}
free(node);
return ptr;
}
void* delete_element(pool_t *pool, int (*cmp) (void *,void *),void *args ){
list_t *l,*l1;
for(l=pool->head , l1 = NULL ; l!=NULL ; l1=l , l=l->next){
if(cmp(l->args,args))
return remove_element(pool, l, l1);
}
return NULL;
}
list_t* search(pool_t *pool, int (*cmp) (void *,void *),void *args){
list_t *node;
for(node = pool->head; node!=NULL && cmp(node->args,args)==0 ; node= node->next);
if(node)
return node;
return NULL;
}
void exec_on_elem(pool_t *pool, void (*exec)(void*)){
list_t *l;
for(l = pool->head ; l != NULL; l = l->next)
exec(l->args);
}
void exec_on_elem_targs(pool_t *pool, void (*exec)(void*, void*), void *args){
list_t *l;
for(l = pool->head ; l != NULL; l = l->next)
exec(args,l->args);
}
void delete_list(pool_t *pool){
list_t *l;
while(pool && pool->head!=pool->tail){
l = pool->head;
pool->head = pool->head->next;
free(l);
}
free(pool->head);
return;
}