1 /*
2  * Copyright (c) 1988, 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Adam de Boor.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #ifndef lint
12 static char sccsid[] = "@(#)lstDestroy.c	8.2 (Berkeley) 04/28/95";
13 #endif /* not lint */
14 
15 /*-
16  * LstDestroy.c --
17  *	Nuke a list and all its resources
18  */
19 
20 #include	"lstInt.h"
21 
22 /*-
23  *-----------------------------------------------------------------------
24  * Lst_Destroy --
25  *	Destroy a list and free all its resources. If the freeProc is
26  *	given, it is called with the datum from each node in turn before
27  *	the node is freed.
28  *
29  * Results:
30  *	None.
31  *
32  * Side Effects:
33  *	The given list is freed in its entirety.
34  *
35  *-----------------------------------------------------------------------
36  */
37 void
38 Lst_Destroy (l, freeProc)
39     Lst	    	  	l;
40     register void	(*freeProc) __P((ClientData));
41 {
42     register ListNode	ln;
43     register ListNode	tln = NilListNode;
44     register List 	list = (List)l;
45 
46     if (l == NILLST || ! l) {
47 	/*
48 	 * Note the check for l == (Lst)0 to catch uninitialized static Lst's.
49 	 * Gross, but useful.
50 	 */
51 	return;
52     }
53 
54     /* To ease scanning */
55     if (list->lastPtr != NilListNode)
56 	list->lastPtr->nextPtr = NilListNode;
57     else {
58 	free ((Address)l);
59 	return;
60     }
61 
62     if (freeProc) {
63 	for (ln = list->firstPtr; ln != NilListNode; ln = tln) {
64 	     tln = ln->nextPtr;
65 	     (*freeProc) (ln->datum);
66 	     free ((Address)ln);
67 	}
68     } else {
69 	for (ln = list->firstPtr; ln != NilListNode; ln = tln) {
70 	     tln = ln->nextPtr;
71 	     free ((Address)ln);
72 	}
73     }
74 
75     free ((Address)l);
76 }
77