File: list.c

package info (click to toggle)
treetool 2.0.2a-4
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 1,172 kB
  • ctags: 2,694
  • sloc: ansic: 26,504; makefile: 215
file content (414 lines) | stat: -rw-r--r-- 8,456 bytes parent folder | download | duplicates (5)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <stdio.h>

#ifdef MEMDBG
#include "memdbg.h"
#endif

typedef struct LIST *list;

typedef struct LIST {
/* a list of objects */
	list obj;		/* the data item, most likely a pointer of some
						type */
	list next;
	list prev;
	} listd;

/* notes:	root->obj!=NULL */

list newlist()
/* return a new list */
{
	list tmp;

	tmp=(list)malloc(sizeof(listd));
	if(tmp!=NULL)
	{
		tmp->next=NULL;
		tmp->prev=NULL;
		tmp->obj=tmp;
	}
	return(tmp);
}

list addnode(root, obj)
/* adds the object to the list after the current node */
/* returns root if ok, 0 if not */
list root;
void *obj;
{
	list tmp;
	list node=root->obj;

	tmp=(list)malloc(sizeof(listd));
	if(tmp!=NULL)
	{
		tmp->obj=obj;
		tmp->next=node->next;
		tmp->prev=node;
		node->next=tmp;
		if(tmp->next!=NULL)
			tmp->next->prev=tmp;
		root->obj=tmp;
		return(root);
	}
	else
		return((list)0);
}

list findnode(root, obj)
/* finds the object in the list, and sets it to be the current node */
/* returns root if ok, 0 if not */
list root;
void *obj;
{
	list node=root->next;

	for(;node!=NULL;node=node->next)
		if(node->obj==obj)
			break;
	if(node!=NULL)
	{
		root->obj=node;
		return(root);
	}
	else
		return((list)0);
}

list rmnode(n)
/* removes the node n from the list */
list n;
{
	list r;

	if(n->prev==NULL)
		return(n);
	
	n->prev->next=n->next;
	if(n->next!=NULL)
		n->next->prev=n->prev;
	r=n->prev;

	free(n);

	return(r);
}

list rmcurr(root)
/* removes the current node from the list */
list root;
{
	list node=root->obj;

	if(node!=root)
	{
		root->obj=node->next;
		if(root->obj==NULL)
			root->obj=node->prev;

		node->prev->next=node->next;
		if(node->next!=NULL)
			node->next->prev=node->prev;
		free(node);
	}
	return(root);
}

int setcurr(root, obj)
list root;
void *obj;
{
	list node=root->obj;

	if(node!=root)
	{
		node->obj=obj;
		return(1);
	}
	else
		return(0);
}

void freelist(root)
/* frees the memory for the list (but not the objects) */
list root;
{
	list tmp;

	while(root!=NULL)
	{
		tmp=root->next;
		free(root);
		root=tmp;
	}
}

list listnode(root)
list root;
/* returns a pointer to the current node */
{
	return(root->obj);
}

list setnode(root, node)
list root, node;
/* sets the current node to be node */
/* returns root if ok, 0 if not */
{
	root->obj=node;
	return(root);
}

list endlist(root)
/* set current to last node in list */
list root;
{
	root->obj=root;
	while(root->obj->next!=NULL)
		root->obj=root->obj->next;
	return(root);
}

list startlist(root)
/* sets current to before first in list */
/* (next will give first node) */
list root;
{
	root->obj=root;
	return(root);
}

list listgonext(root)
/* sets current to next node in list */
list root;
{
	if(root->obj->next!=NULL)
		root->obj=root->obj->next;
	return(root);
}

list lastnode(root)
/* returns last node in list */
list root;
{
	if(root!=NULL)
		while(root->next!=NULL)
			root=root->next;
	return(root);
}

list listgoprev(root)
/* sets current to the previous node in list */
list root;
{
	if(root->obj->prev!=root)
		root->obj=root->obj->prev;
	return(root);
}

void *listnext(root)
/* sets current to next node in list, and returns that object */
list root;
{
	if(root->obj->next==NULL)
		return(NULL);
	else
		return((root->obj=root->obj->next)->obj);
}

void *listprev(root)
/* sets current to the previous node in list, and returns that object */
list root;
{
	if(root->obj->prev==root)
		return(NULL);
	else
		return((root->obj=root->obj->prev)->obj);
}

void *listcurr(root)
/* returns the current object */
list root;
{
	if(root->obj==root)
		return(NULL);
	else
		return(root->obj->obj);
}

int listlastp(root)
/* returns true if current is last in list */
list root;
{
	return(root->obj->next==NULL);
}

int listfirstp(root)
/* returns true if current is first in list */
list root;
{
	return(root->obj->prev==root);
}

int listemptyp(root)
/* returns true if there is nothing in the list */
list root;
{
	return(root->next==NULL);
}

#ifdef TESTING
printlist(root)
list root;
{
	list tmp;

	printf("(");
	for(tmp=root->next;tmp!=NULL;tmp=tmp->next)
		printf("%s%d ", tmp==root->obj?"+":"", tmp->obj);
	printf(")\n");
}
error(string)
char *string;
{
	printf("Error: %s\n", string);
	exit(0);
}
t_list()
{
	list this;

	this=newlist();
	printf("should be: ()\n");
	printlist(this);
	if(listcurr(this)!=NULL)
		error("listcurr(newlist())!=NULL");
	if(!listemptyp(this))
		error("listemptyp(newlist()) not true");
	addnode(this, (void *)25);
	printf("should be: (+25)\n");
	printlist(this);
	if(listemptyp(this))
		error("list empty after adding a node");
	if(!listlastp(this))
		error("listlastp says no when at end of list");
	addnode(this, (void *)50);
	printf("should be: (25 +50)\n");
	printlist(this);
	if(listcurr(this)!=(void *)50)
		error("not setting current on addnode");
	if(!listlastp(this))
		error("listlastp=no when at end of list, or not adding after node");
	rmcurr(this);
	printf("should be: (+25)\n");
	printlist(this);
	if(listcurr(this)!=(void *)25)
		error("rmcurr didn't work, or not setting current on rmcurr");
	rmcurr(this);
	printf("should be: ()\n");
	printlist(this);
	if(listcurr(this)!=NULL)
		error("after removing all nodes, listcurr doesn't say NULL");
	if(!listemptyp(this))
		error("list not empty after addnode and rmcurr");
	if(listnext(this)!=NULL)
		error("after removing all nodes, listnext doesn't say NULL");
	addnode(this, (void *)25);
	printf("should be: (+25)\n");
	printlist(this);
	if(listnext(this)!=NULL)
		error("current not reset after addnode, or problem with listnext");
	if(listcurr(this)!=(void *)25)
		error("problem with listcurr, or listnext moves at end of list");
	if(listnext(startlist(this))!=(void *)25)
		error("addnode(25);listnext(startlist(this))!=25");
	if(listnext(this)!=NULL)
		error("listnext doesn't return NULL at end of list");
	if(!listfirstp(this))
		error("listfirstp says no at first in list");
	if(!listlastp(this))
		error("listlastp says no at end of list");
	startlist(this);
	addnode(this, (void *)50);
	printf("should be: (+50 25)\n");
	printlist(this);
	if(listcurr(this)!=(void *)50)
		error("startlist();addnode(50);listcurr()!=50");
	if(!listfirstp(this))
		error("startlist();addnode(50);listfirstp() says no");
	if(listnext(this)!=(void *)25)
		error("new();add(25);start();add(50);next()!=25");
	printf("should be: (50 +25)\n");
	printlist(this);
	if(listprev(this)!=(void *)50)
		error("addnode(25);startlist();addnode(50);listnext();listprev()!=50");
	printf("should be: (+50 25)\n");
	printlist(this);
	if(!listfirstp(this))
		error("listfirstp says no at first in list");
	listnext(this);
	printf("should be: (50 +25)\n");
	printlist(this);
	if(!listlastp(this))
		error("listlastp says no at end of list");
	listprev(this);
	printf("should be: (+50 25)\n");
	printlist(this);
	addnode(this, (void *)75);
	printf("should be: (50 +75 25)\n");
	printlist(this);
	listprev(this);
	if(listcurr(this)!=(void *)50)
		error("(+50 25);add(75);prev()!=50");
	if(listnext(this)!=(void *)75)
		error("(+50 25);add(75);prev();next()!=75");
	if(listnext(this)!=(void *)25)
		error("(+50 25);add(75);prev();next();next()!=25");
	startlist(this);
	printf("should be: (50 75 25)\n");
	printlist(this);
	if(listcurr(this)!=NULL)
		error("(50 75 +25);start();listcurr()!=NULL");
	endlist(this);
	if(listcurr(endlist(this))!=(void *)25)
		error("(50 75 25);end();listcurr(end())!=25");
	endlist(this);
	if(listcurr(this)!=(void *)25)
		error("(50 75 +25);end();listcurr()!=25,end fails when at end");
	printf("should be: (50 75 +25)\n");
	printlist(this);
	{
		list tmp;

		tmp=listnode(this);
		if(listprev(listgoprev(this))!=(void *)50)
			error("(50 75 +25);prev(goprev())!=50");
		printf("should be: (+50 75 25)\n");
		printlist(this);
		if(listcurr(listgonext(this))!=(void *)75)
			error("(+50 75 25);curr(gonext())!=75");
		printf("should be: (50 +75 25)\n");
		printlist(this);
		if(listcurr(setnode(this, tmp))!=(void *)25)
			error("(50 75 +25);tmp=listnode();curr(setnode(tmp))!=25");
		printf("should be: (50 75 +25)\n");
		printlist(this);
	}
	rmcurr(this);
	rmcurr(this);
	rmcurr(this);
	printf("should be: ()\n");
	printlist(this);
	if(!listemptyp(this))
		error("(50 75 25);end();rmcurr*3();emptyp() not true");
	if(listcurr(endlist(this))!=NULL)
		error("endlist() fails with empty list");
	freelist(this);
	this=newlist();
	for(i=1;i<40;i++)
		addnode(this, (void *)i);
	printlist(this);
	printf("Sacrifice to the gods!  It all works!\n");
}
#endif /* TESTING */