-
Notifications
You must be signed in to change notification settings - Fork 31
/
cslist.c
261 lines (213 loc) · 4.98 KB
/
cslist.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
/*
* _____
* ANSI / ___/
* / /__
* \___/
*
* Filename: clist.c
* Author : Kyle Loudon/Dan Levin
* Date : Mon Apr 08 12:24:12 2013
* Version : 0.51
* ---
* Description: A circular, singly-linked list - implemented as a pure, generic ADT
*
* Date Revision message
* 130413 Created this file
* 150331 This code ready for version 0.51
*
*/
/**
* @file cslist.c
**/
#include <stdio.h>
#include <stdlib.h>
#include "cslist.h"
struct CSListElmt_
{
void *data;
struct CSListElmt_ *next;
};
struct CSList_
{
int size;
int (*match)(const void *key1, const void *key2);
void (*destroy)(void *data);
struct CSListElmt_ *head;
};
/* FUNCTION DEFINITIONS --------------------------------------------------- */
CSlist CSLISTinit(void (*destroy)(void *data))
{
CSlist clist;
if ((clist = (CSlist)malloc(sizeof(struct CSList_)))==NULL)
return NULL;
clist->size = 0;
clist->match = NULL;
clist->destroy = destroy;
clist->head = NULL;
return clist;
}
void CSLISTdestroy(CSlist clist)
{
void *data;
while (CSLISTsize(clist) > 0)
{
if (CSLISTremnext(clist, clist->head, (void **)&data) == 0 && clist->destroy !=
NULL)
clist->destroy(data);
}
free(clist);
}
int CSLISTinsnext(CSlist clist, CSlistNode node, const void *data)
{
CSlistNode newnode;
/* Allocate storage for new node */
if ((newnode = (CSlistNode)malloc(sizeof(struct CSListElmt_))) == NULL)
return -1;
newnode->data = (void *)data;
if (CSLISTsize(clist) == 0)
{
/* Handle insertion when the list is empty */
newnode->next = newnode;
clist->head = newnode;
}
else
{
/* Handle insertion when the list is not empty */
newnode->next = node->next;
node->next = newnode;
}
/* Adjust list size to account for the new node */
clist->size++;
return 0;
}
int CSLISTremnext(CSlist clist, CSlistNode node, void **data)
{
CSlistNode oldnode;
/* Don't allow removal from an empty list */
if (clist->size == 0)
return -1;
/* Remove the node from the list */
*data = node->next->data;
if (node->next == node)
{
/* Handle removing the last element */
oldnode = node->next;
clist->head = NULL;
}
else
{
/* Handle removing other than the last element */
oldnode = node->next;
node->next = node->next->next;
if (oldnode == clist->head)
clist->head = oldnode->next;
}
/* Free storage occupied by the old element */
free(oldnode);
/* Adjust list size to account for the removal */
clist->size--;
return 0;
}
int CSLISTsize(CSlist clist)
{
return clist->size;
}
CSlistNode CSLISThead(CSlist clist)
{
return clist->head;
}
int CSLISTishead(CSlist clist, CSlistNode node)
{
return clist->head==node ? 1 : 0;
}
void *CSLISTdata(CSlistNode node)
{
return node->data;
}
CSlistNode CSLISTnext(CSlistNode node)
{
return node->next;
}
CSlistNode CSLISTfindnode(CSlist clist, const void *data)
{
CSlistNode member = NULL;
int head_hit;
/* Match callback not set - or empty list - not allowed.. */
if (clist->match == NULL || clist->size == 0)
return NULL;
member = clist->head;
head_hit = 0;
do
{
if (clist->match(data, CSLISTdata(member)))
{
if (member == clist->head)
head_hit = 1;
break;
}
member = member->next;
} while (member != clist->head);
if (member == clist->head && !head_hit) /* Node not found */
return NULL;
else
return member; /* We have a hit - somewhere in the list... */
}
void CSLISTsetmatch(CSlist clist, int (*match)(const void *key1, const void *key2))
{
clist->match = match;
}
int CSLISTfind_remove(CSlist clist, void **data)
{
CSlistNode member, prev;
int head_hit;
/* If match-callback not set */
if (clist->match == NULL)
return -2;
/* If list empty.. - node can not be found! */
if (!clist->size)
return 1;
/* Initialize 'prev' with node prior to head node... */
prev = clist->head;
while (prev->next != clist->head)
prev = prev->next;
member = clist->head;
head_hit = 0;
do
{
if (clist->match(*data, CSLISTdata(member)))
{
if (member == clist->head)
head_hit = 1;
break;
}
prev = member;
member = member->next;
} while (member != clist->head);
/* Node not found */
if (member == clist->head && !head_hit)
return 1;
/* Perform the removal.. */
if (clist->size == 1)
{
*data = clist->head->data;
free(clist->head);
clist->head = NULL;
clist->size--;
return 0;
}
else
return CSLISTremnext(clist, prev, data);
}
void CSLISTtraverse(CSlist clist, void (*callback)(const void *data))
{
CSlistNode curr;
if (clist->size)
{
curr = clist->head;
do
{
callback(curr->data);
curr = curr->next;
} while (curr != clist->head);
}
}