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
|
/* listsetc.c General routines for lists, etc */
#include <stdlib.h>
#include <stdio.h>
#include "listsetc.h"
/**********************************************************************/
/* lbs_init() create and initialise a list/bag/set */
/* return a pointer to the new list */
LBS_PTR lbs_init()
{
LBS_PTR newlist; /* pointer to new list */
/* create the list */
newlist = (LBS *) malloc(sizeof(LBS));
if (newlist == NULL) {
return(NULL);
}
/* initialise the list */
FRONT(newlist) = NULL;
BACK(newlist) = NULL;
NELS(newlist) = 0;
return(newlist);
} /* end LBS_INIT */
/**********************************************************************/
/**********************************************************************/
/* alloc_lbsnode() allocate space for a (list) node */
/* return a pointer to the node structure */
LBS_NODE_PTR alloc_lbsnode()
{
LBS_NODE_PTR newnode;
/* create the node */
newnode = (LBS_NODE *) malloc(sizeof(LBS_NODE));
if (newnode == NULL) {
return(NULL);
}
DATA(newnode) = NULL;
NEXTEL(newnode) = NULL;
return(newnode);
} /* end ALLOC_LBSNODE */
/**********************************************************************/
/**********************************************************************/
/* free_lbsnode() free a (list) node */
void free_lbsnode(pN)
LBS_NODE_PTR pN;
{
free(pN);
pN = NULL;
} /* end FREE_LBSNODE */
/**********************************************************************/
/**********************************************************************/
/* lbs_empty(pL) TRUE iff list is empty */
int lbs_empty(pL)
LBS_PTR pL;
{
if (pL == NULL) return(TRUE);
if (FRONT(pL) == NULL) {
return(TRUE);
}
else {
return(FALSE);
}
} /* end LBS_EMPTY */
/**********************************************************************/
/**********************************************************************/
/* lbs_prepend() add an element to the front of a list */
/* return pointer to new element node */
LBS_NODE_PTR lbs_prepend(pL, data)
LBS_PTR pL;
genptr data;
{
LBS_NODE_PTR newnode;
if (pL == NULL) return(NULL);
/* allocate a new node */
if ((newnode = alloc_lbsnode()) == NULL) {
return(NULL);
}
/* put the data into the node */
DATA(newnode) = data;
if (lbs_empty(pL)) {
FRONT(pL) = newnode;
BACK(pL) = newnode;
}
else {
NEXTEL(newnode) = FRONT(pL);
FRONT(pL) = newnode;
}
NELS(pL) = NELS(pL) + 1;
return(newnode);
} /* end LBS_PREPEND */
/**********************************************************************/
/**********************************************************************/
/* lbs_append() add an element at the end of a list */
/* return pointer to new node */
LBS_NODE_PTR lbs_append(pL, data)
LBS_PTR pL;
genptr data;
{
LBS_NODE_PTR newnode;
if (pL == NULL) return(NULL);
/* allocate a new node */
if ((newnode = alloc_lbsnode()) == NULL) {
return(NULL);
}
/* put the data into the node */
DATA(newnode) = data;
if (lbs_empty(pL)) {
FRONT(pL) = newnode;
BACK(pL) = newnode;
}
else {
NEXTEL(BACK(pL)) = newnode;
BACK(pL) = newnode;
}
NELS(pL) = NELS(pL) + 1;
return(newnode);
} /* end LBS_APPEND */
/**********************************************************************/
/**********************************************************************/
/* lbs_insert() inserts a new element after the pos'th element */
/* return pointer to the new node */
LBS_NODE_PTR lbs_insert(pL, data, pos)
LBS_PTR pL; /* the list */
genptr data; /* the data */
int pos; /* the position, starting from 1 */
{
LBS_NODE_PTR nod, previous, next;
LBS_NODE_PTR newnode;
int i;
int loc = pos;
if (pL == NULL) return(NULL);
if (pos <= 0) loc = 0;
/* allocate a new node */
if ((newnode = alloc_lbsnode()) == NULL) {
return(NULL);
}
/* put the data into the node */
DATA(newnode) = data;
if (lbs_empty(pL)) { /* easy */
FRONT(pL) = newnode;
BACK(pL) = newnode;
NELS(pL) = NELS(pL) + 1;
return(newnode);
}
if (loc == 0) { /* easy */
NEXTEL(newnode) = FRONT(pL);
FRONT(pL) = newnode;
NELS(pL) = NELS(pL) + 1;
return(newnode);
}
if (loc >= NELS(pL)) { /* add at end */
NEXTEL(BACK(pL)) = newnode;
BACK(pL) = newnode;
NELS(pL) = NELS(pL) + 1;
return(newnode);
}
nod = FRONT(pL);
previous = nod;
for (i = 1; i <= pos; i++) { /* traverse the list */
if (NEXTEL(nod) == NULL) { /* going past end of the list */
break;
}
previous = nod;
nod = NEXTEL(previous);
}
/* previous is the node to be added after */
if (previous == BACK(pL)) { /* at the end */
NEXTEL(previous) = newnode;
BACK(pL) = newnode;
}
else { /* in the middle */
NEXTEL(newnode) = NEXTEL(previous);
NEXTEL(previous) = newnode;
}
NELS(pL) = NELS(pL) + 1;
return(newnode);
} /* end LBS_INSERT */
/**********************************************************************/
/**********************************************************************/
/* lbs_remove() deletes the pos'th element from a list */
/* returns pointer to removed node */
LBS_NODE_PTR lbs_remove(pL, pos)
LBS_PTR pL; /* the list */
int pos; /* the position, starting from 1 */
{
LBS_NODE_PTR nod, previous, next;
int i;
if (pL == NULL) return(NULL);
if (lbs_empty(pL)) { /* nothing to delete */
return(NULL);
}
nod = FRONT(pL);
previous = nod;
for (i = 1; i < pos; i++) { /* traverse the list */
if (NEXTEL(nod) == NULL) { /* going past end of the list */
return(NULL);
}
previous = nod;
nod = NEXTEL(previous);
}
/* nod is the node to be removed */
if (nod == FRONT(pL)) { /* deleting first in the list */
if (nod == BACK(pL)) { /* deleting the only member */
FRONT(pL) = NULL;
BACK(pL) = NULL;
}
else {
FRONT(pL) = NEXTEL(nod);
}
}
else if (nod == BACK(pL)) { /* deleting last element */
NEXTEL(previous) = NULL;
BACK(pL) = previous;
}
else { /* deleting in the middle */
NEXTEL(previous) = NEXTEL(nod);
}
NELS(pL) = NELS(pL) - 1;
return(nod);
} /* end LBS_REMOVE */
/**********************************************************************/
/**********************************************************************/
/* lbs_get_first get first element */
/* returns pointer to the node */
LBS_NODE_PTR lbs_get_first(pL)
LBS_PTR pL;
{
if (pL == NULL) return(NULL);
return(FRONT(pL));
} /* end LBS_GET_FIRST */
/**********************************************************************/
/**********************************************************************/
/* lbs_get_last get last element */
/* returns pointer to the node */
LBS_NODE_PTR get_last(pL)
LBS_PTR pL;
{
if (pL == NULL) return(NULL);
return(BACK(pL));
} /* end LBS_GET_LAST */
/**********************************************************************/
/**********************************************************************/
/* lbs_get_nth get n'th element */
/* returns pointer to the node */
LBS_NODE_PTR lbs_get_nth(pL, n)
LBS_PTR pL;
int n;
{
LBS_NODE_PTR nth;
int i;
if (pL == NULL) return(NULL);
if (n <= 0) return(NULL);
if (n == 1) return(FRONT(pL));
if (n == NELS(pL)) return(BACK(pL));
nth = FRONT(pL);
for (i = 1; i < n; i++) {
nth = NEXTEL(nth);
if (nth == NULL) { /* past the end */
return(NULL);
}
}
return(nth);
} /* end LBS_GET_NTH */
/**********************************************************************/
/**********************************************************************/
/* lbs_get_next_el get next element */
/* returns pointer to the node */
LBS_NODE_PTR lbs_get_next_el(pL, this)
LBS_PTR pL;
LBS_NODE_PTR this; /* current node, NULL to start off */
{
if (pL == NULL) return(NULL);
if (this == NULL) return(FRONT(pL));
return(NEXTEL(this));
} /* end LBS_GET_NEXT_EL */
/**********************************************************************/
|