1 |
/* Copyright (c) 1998 Silicon Graphics, Inc. */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ SGI"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* sm_list.c |
9 |
* Routines for handling linked generic linked lists, stack, and |
10 |
* circular lists. Data element is an int (can be cast to hold |
11 |
* pointer to more general data element) |
12 |
*/ |
13 |
|
14 |
#include "standard.h" |
15 |
#include "sm_list.h" |
16 |
|
17 |
/* List of available edges */ |
18 |
static LIST *free_lists = NULL; |
19 |
#ifdef DEBUG |
20 |
extern int Malloc_cnt; |
21 |
#endif |
22 |
LIST |
23 |
*new_list() |
24 |
{ |
25 |
LIST *l; |
26 |
|
27 |
|
28 |
|
29 |
/* check free list for available edges */ |
30 |
if( free_lists ) |
31 |
{ |
32 |
l = free_lists; |
33 |
free_lists = LIST_NEXT(free_lists); |
34 |
} |
35 |
/* if no free edges- allocate the memory */ |
36 |
else |
37 |
{ |
38 |
if( !(l = (LIST *)malloc(sizeof(LIST)))) |
39 |
error(SYSTEM,"new_list():Unable to allocate memory"); |
40 |
} |
41 |
/* clear the memory */ |
42 |
bzero(l, sizeof(LIST)); |
43 |
|
44 |
return(l); |
45 |
} |
46 |
|
47 |
|
48 |
/* attaches list a at the end of list b */ |
49 |
LIST |
50 |
*append_list(a,b) |
51 |
LIST *a,*b; |
52 |
{ |
53 |
LIST * l; |
54 |
|
55 |
if(!a) |
56 |
return(b); |
57 |
|
58 |
if(!b) |
59 |
return(a); |
60 |
|
61 |
l = b; |
62 |
while(LIST_NEXT(l) && LIST_NEXT(l) != b) |
63 |
l = LIST_NEXT(l); |
64 |
|
65 |
SET_LIST_NEXT(l,a); |
66 |
|
67 |
return(b); |
68 |
} |
69 |
|
70 |
/* Adds data to the end of a circular list. If set, "end" |
71 |
* is a pointer to the last element in the list |
72 |
*/ |
73 |
LIST |
74 |
*add_data_to_circular_list(l,end,d) |
75 |
LIST *l,**end; |
76 |
int d; |
77 |
{ |
78 |
LIST *list; |
79 |
|
80 |
list = new_list(); |
81 |
SET_LIST_DATA(list,d); |
82 |
|
83 |
if(!l) |
84 |
l = list; |
85 |
else |
86 |
{ |
87 |
if(*end) |
88 |
SET_LIST_NEXT(*end,list); |
89 |
else |
90 |
l = append_list(list,l); |
91 |
} |
92 |
|
93 |
*end = list; |
94 |
SET_LIST_NEXT(list,l); |
95 |
return(l); |
96 |
} |
97 |
|
98 |
/* Pushes data element d at the top of the list- returns pointer |
99 |
to new top of list |
100 |
*/ |
101 |
LIST |
102 |
*push_data(l,d) |
103 |
LIST *l; |
104 |
int d; |
105 |
{ |
106 |
LIST * list; |
107 |
|
108 |
list = new_list(); |
109 |
SET_LIST_DATA(list,d); |
110 |
SET_LIST_NEXT(list,l); |
111 |
return(list); |
112 |
} |
113 |
|
114 |
/* frees the list */ |
115 |
LIST |
116 |
*free_list(l) |
117 |
LIST * l; |
118 |
{ |
119 |
if(!l) |
120 |
return(NULL); |
121 |
free_lists = append_list(free_lists,l); |
122 |
|
123 |
return(NULL); |
124 |
} |
125 |
|
126 |
/* Returns data element d at the top of the list- returns pointer |
127 |
to new top of list |
128 |
*/ |
129 |
int |
130 |
pop_list(l) |
131 |
LIST **l; |
132 |
{ |
133 |
LIST *p; |
134 |
void *d; |
135 |
|
136 |
if(!l) |
137 |
return(NULL); |
138 |
if(!(p = *l)) |
139 |
return(NULL); |
140 |
|
141 |
d = LIST_DATA(p); |
142 |
|
143 |
*l = LIST_NEXT(p); |
144 |
|
145 |
LIST_NEXT(p) =NULL; |
146 |
free_list(p); |
147 |
|
148 |
return(d); |
149 |
} |
150 |
|
151 |
/* Search through list for data element "d", and remove from |
152 |
* list: Returns TRUE if found, false otherwise |
153 |
*/ |
154 |
int |
155 |
remove_from_list(d,list) |
156 |
int d; |
157 |
LIST **list; |
158 |
{ |
159 |
LIST *l,*prev; |
160 |
|
161 |
l = *list; |
162 |
prev = NULL; |
163 |
|
164 |
while(l) |
165 |
{ |
166 |
if(LIST_DATA(l) == d) |
167 |
{ |
168 |
if(l == *list) |
169 |
*list = LIST_NEXT(*list); |
170 |
else |
171 |
SET_LIST_NEXT(prev,LIST_NEXT(l)); |
172 |
|
173 |
SET_LIST_NEXT(l,NULL); |
174 |
free_list(l); |
175 |
return(TRUE); |
176 |
} |
177 |
prev = l; |
178 |
l = LIST_NEXT(l); |
179 |
} |
180 |
return(FALSE); |
181 |
} |
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|