File: list.c

package info (click to toggle)
atari800 0.8.6-2
  • links: PTS
  • area: contrib
  • in suites: hamm
  • size: 1,364 kB
  • ctags: 2,457
  • sloc: ansic: 22,055; asm: 2,812; cpp: 581; makefile: 68; sh: 8
file content (505 lines) | stat: -rw-r--r-- 10,177 bytes parent folder | download
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/*
 * Module:       Linked List
 * Author:       David J. Firth
 * Date Written: A long time ago!
 * Version:      1.0
 *
 * Description
 * -----------
 *
 * This module contains a series of general purpose routines for the
 * manipulation of Linked Lists.
 */

/**
  The "List" library contains a set of routines that create and
  manipulate Linked Lists.
  **/

#include <stdio.h>
#include <stdlib.h>

static char *rcsid = "$Id: list.c,v 1.3 1998/02/21 14:56:45 david Exp $";

#include "list.h"

#define	FALSE 0
#define	TRUE 1

/*+
   Creates an empty List

   List *ListCreate Returns an empty list.
   + */

List *ListCreate(void)
{
	List *list;

	list = (List *) malloc(sizeof(List));
	if (list) {
		list->head = NULL;
		list->tail = NULL;
		list->current = NULL;
		list->next = NULL;
	}
	return list;
}

/*+
   Releases all storages allocated by the supplied List. The data contained
   in each node should be released by the function specified in the
   argument list.

   int ListFree returns TRUE on success and FALSE on failure.

   List *list The List to deallocate.

   void (*func)() The function that deallocates the data. The deallocation
   function is called with a single argument (the data). The free() function
   can be specified if the data is a pointer to an array of characters. A
   NULL value can be supplied if a deallocation function is not necessary.
   + */

int ListFree(List * list, void (*func) ())
{
	struct list_entry *temp;
	struct list_entry *next;

	int ok = FALSE;

	if (list) {
		for (temp = list->head; temp; temp = next) {
			next = temp->next;
			if (func)
				(*func) (temp->data);
			free(temp);
		}

		free(list);

		ok = TRUE;
	}
	return ok;
}

/*+
   Adds the supplied data item to Head of List.

   int ListAddHead Returns TRUE on success and FALSE on failure.

   List *list The list that the data item is to be added to.

   void *data Data item that is added to the list. This can be a pointer to
   anything, and with care a simple integer value.
   + */

int ListAddHead(List * list, void *data)
{
	struct list_entry *temp;

	int ok = FALSE;

	if (list) {
		temp = (struct list_entry *) malloc(sizeof(struct list_entry));
		if (temp) {
			temp->prev = NULL;
			temp->next = list->head;
			temp->data = data;

			if (list->head) {
				list->head->prev = temp;
				list->head = temp;
			}
			else {
				list->head = list->tail = temp;
			}

			ok = TRUE;
		}
	}
	return ok;
}

/*+
   Adds the supplied data item to Tail of List.

   int ListAddTail Returns TRUE on success and FALSE on failure.

   List *list The list that the data item is to be added to.

   void *data Data item that is added to the list. This can be a pointer to
   anything, and with care a simple integer value.
   + */

int ListAddTail(List * list, void *data)
{
	struct list_entry *temp;

	int ok = FALSE;

	if (list) {
		temp = (struct list_entry *) malloc(sizeof(struct list_entry));
		if (temp) {
			temp->prev = list->tail;
			temp->next = NULL;
			temp->data = data;

			if (list->tail) {
				list->tail->next = temp;
				list->tail = temp;
			}
			else {
				list->head = list->tail = temp;
			}

			ok = TRUE;
		}
	}
	return ok;
}

/*+
   Merges list1 and list2. This is performed by appending list2 onto
   list1. On return from this routine, list2 will have been de-allocated
   and the return value will be the same as list1.

   List *ListMerge list1 with list2 appended.

   List *list1 This is the primary list.

   List *list2 This is list that will be appended to the primary list.
   + */

List *ListMerge(List * list1, List * list2)
{
	if (!list1->head) {
		list1->head = list2->head;
		list1->tail = list2->tail;
	}
	else if (list2->head) {
		list1->tail->next = list2->head;
		list2->head->prev = list1->tail;
		list1->tail = list2->tail;
	}
	list2->head = NULL;
	list2->tail = NULL;

	ListFree(list2, NULL);

	return list1;
}

/*+
   ListSort() is called to sort a list into order.

   List *list List that is to be sorted.

   int (*func)() Pointer to a function that compares two entries in the list.
   The function should return -1 if entry1 < entry2, 0 if entry1 = entry2
   and +1 if entry1 > entry2.
   + */

void ListSort(List * list, int (*func) ())
{
	struct list_entry *next;
	struct list_entry *temp;

	void *data;
	int done;
	int result;

	if (list->head) {
		done = FALSE;

		while (!done) {
			done = TRUE;

			for (temp = list->head, next = temp->next; next; temp = next, next = temp->next) {
				result = (*func) (temp->data, next->data);
				if (result == 1) {
					data = temp->data;
					temp->data = next->data;
					next->data = data;

					done = FALSE;
				}
			}
		}
	}
}

/*+
   Resets current node back to head node.

   int ListReset returns TRUE on success and FALSE on failure.

   List *list The list to be reset.
   + */

int ListReset(List * list)
{
	int status = FALSE;

	if (list) {
		list->current = NULL;
		list->prev = list->tail;
		list->next = list->head;
		status = TRUE;
	}
	return status;
}

/*+
   This routine is used to traverse a list. Each time it is called the
   next item in the list is returned. If ListReset() is called beforehand
   then ListTraverse() returns the head node.

   int ListTraverse Returns TRUE indicates that the next entry has been
   returned. FALSE indicates that the end of the list has been
   reached and no entry has been returned.

   List *list The list to be traversed.

   void **entry This is an output variable and is used to store the address
   of the next data item in the list.
   + */

int ListTraverse(List * list, void **entry /*+ Current Entry + */ )
{
	struct list_entry *next;

	int status;

	next = list->next;
	if (next) {
		*entry = next->data;
		list->current = next;
		list->prev = next->prev;
		list->next = next->next;
		status = TRUE;
	}
	else {
		status = FALSE;
	}

	return status;
}

/*+
   This routine is used to traverse a list. Each time it is called the
   previous item in the list is returned. If ListReset() is called
   beforehand then ListTraverseBck() returns the tail node.

   int ListTraverseBck Returns TRUE indicates that the previous entry has
   been returned. FALSE indicates that the begining of the list has been
   reached and no entry has been returned.

   List *list The list to be traversed.

   void **entry This is an output variable and is used to store the address
   of the previous data item in the list.
   + */

int ListTraverseBck(List * list, void **entry /*+ Current Entry + */ )
{
	struct list_entry *prev;

	int status;

	prev = list->prev;
	if (prev) {
		*entry = prev->data;
		list->current = prev;
		list->prev = prev->prev;
		list->next = prev->next;
		status = TRUE;
	}
	else {
		status = FALSE;
	}

	return status;
}

/*
   =====================================================
   Routines to be called from within a ListTraverse Loop
   =====================================================
 */

/*+
   Deletes the current entry in the supplied linked list. Used in
   conjunction with ListTraverse().

   int ListDeleteEntry Returns TRUE on success and FALSE on failure.

   List *list The current entry in this list will be deleted.
   + */

int ListDeleteEntry(List * list)
{
	int status = FALSE;

	if (list) {
		struct list_entry *current;

		current = list->current;
		if (current) {
			struct list_entry *prev;
			struct list_entry *next;

			prev = current->prev;
			next = current->next;

			free(current);

			if (prev)
				prev->next = next;
			else
				list->head = next;

			if (next)
				next->prev = prev;
			else
				list->tail = prev;

			status = TRUE;
		}
	}
	return status;
}

/*+
   Inserts the supplied data item just before the current entry in the
   supplied list. Used in conjunction with ListTraverse().

   int ListInsertBefore Returns TRUE on success and FALSE on failure.

   List *list The data item will be inserted before the current entry
   in this list.

   void *data This is the data item that will be inserted into the list.
   + */

int ListInsertBefore(List * list, void *data)
{
	int status = FALSE;

	if (list) {
		if (list->current) {
			struct list_entry *current;
			struct list_entry *prev;
			struct list_entry *temp;

			current = list->current;
			prev = current->prev;

			temp = (struct list_entry *) malloc(sizeof(struct list_entry));
			if (temp) {
				temp->prev = prev;
				temp->next = current;
				temp->data = data;

				current->prev = temp;

				if (prev) {
					prev->next = temp;
				}
				else {
					list->head = temp;
				}

				status = TRUE;
			}
		}
	}
	return status;
}

/*+
   Inserts the supplied data item just after the current entry in the
   supplied list. Used in conjunction with ListTraverse().

   int ListInsertAfter Returns TRUE on success and FALSE on failure.

   List *list The data item will be inserted after the current entry
   in this list.

   void *data This is the data item that will be inserted into the list.
   + */

int ListInsertAfter(List * list, void *data)
{
	int status = FALSE;

	if (list) {
		if (list->current) {
			struct list_entry *current;
			struct list_entry *next;
			struct list_entry *temp;

			current = list->current;
			next = current->next;

			temp = (struct list_entry *) malloc(sizeof(struct list_entry));
			if (temp) {
				temp->prev = current;
				temp->next = next;
				temp->data = data;

				current->next = temp;

				if (next) {
					next->prev = temp;
				}
				else {
					list->tail = temp;
				}

				status = TRUE;
			}
		}
	}
	return status;
}

/*+
   Swaps two adjacent items in the list. The two items that are swapped are
   the current entry and the next entry. Used in conjunction with
   ListTraverse().

   int ListSwapEntry Returns TRUE on success and FALSE on failure.

   List *list List containing the items to be swapped.
   + */

int ListSwapEntry(List * list)
{
	int status = FALSE;

	if (list) {
		if (list->current) {
			struct list_entry *current;

			current = list->current;

			if (current) {
				struct list_entry *prev;

				prev = current->prev;

				if (prev) {
					void *temp;

					temp = prev->data;
					prev->data = current->data;
					current->data = temp;

					status = TRUE;
				}
			}
		}
	}
	return status;
}