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 |
/* check free list for available edges */ |
28 |
if( free_lists ) |
29 |
{ |
30 |
l = free_lists; |
31 |
free_lists = LIST_NEXT(free_lists); |
32 |
} |
33 |
/* if no free edges- allocate the memory */ |
34 |
else |
35 |
{ |
36 |
if( !(l = (LIST *)malloc(sizeof(LIST)))) |
37 |
error(SYSTEM,"new_list():Unable to allocate memory"); |
38 |
} |
39 |
/* clear the memory */ |
40 |
bzero(l, sizeof(LIST)); |
41 |
|
42 |
return(l); |
43 |
} |
44 |
|
45 |
|
46 |
/* attaches list a at the end of list b */ |
47 |
LIST |
48 |
*append_list(a,b) |
49 |
LIST *a,*b; |
50 |
{ |
51 |
LIST * l; |
52 |
|
53 |
if(!b) |
54 |
return(a); |
55 |
|
56 |
l = b; |
57 |
while(LIST_NEXT(l) && LIST_NEXT(l) != b) |
58 |
l = LIST_NEXT(l); |
59 |
|
60 |
SET_LIST_NEXT(l,a); |
61 |
|
62 |
return(b); |
63 |
} |
64 |
|
65 |
/* attaches list a at the end of list b */ |
66 |
LIST |
67 |
*add_data(l,d,end) |
68 |
LIST *l; |
69 |
int d; |
70 |
LIST **end; |
71 |
|
72 |
{ |
73 |
LIST *list,*lptr; |
74 |
|
75 |
list = new_list(); |
76 |
SET_LIST_DATA(list,d); |
77 |
|
78 |
if(!l) |
79 |
{ |
80 |
if(end) |
81 |
*end = list; |
82 |
return(list); |
83 |
} |
84 |
if(end) |
85 |
lptr = *end; |
86 |
else |
87 |
{ |
88 |
lptr = l; |
89 |
while(LIST_NEXT(lptr)) |
90 |
lptr = LIST_NEXT(lptr); |
91 |
} |
92 |
LIST_NEXT(lptr) = list; |
93 |
if(end) |
94 |
*end = list; |
95 |
|
96 |
return(l); |
97 |
} |
98 |
|
99 |
/* Adds data to the end of a circular list. If set, "end" |
100 |
* is a pointer to the last element in the list |
101 |
*/ |
102 |
LIST |
103 |
*add_data_to_circular_list(l,end,d) |
104 |
LIST *l,**end; |
105 |
int d; |
106 |
{ |
107 |
LIST *list; |
108 |
|
109 |
list = new_list(); |
110 |
SET_LIST_DATA(list,d); |
111 |
|
112 |
if(!l) |
113 |
l = list; |
114 |
else |
115 |
{ |
116 |
if(*end) |
117 |
SET_LIST_NEXT(*end,list); |
118 |
else |
119 |
l = append_list(list,l); |
120 |
} |
121 |
|
122 |
*end = list; |
123 |
SET_LIST_NEXT(list,l); |
124 |
return(l); |
125 |
} |
126 |
|
127 |
|
128 |
|
129 |
/* frees the list */ |
130 |
LIST |
131 |
*free_list(l) |
132 |
LIST * l; |
133 |
{ |
134 |
if(!l) |
135 |
return(NULL); |
136 |
|
137 |
free_lists = append_list(free_lists,l); |
138 |
|
139 |
return(NULL); |
140 |
} |
141 |
|
142 |
/* Pushes data element d at the top of the list- returns pointer |
143 |
to new top of list |
144 |
*/ |
145 |
LIST |
146 |
*push_data(l,d) |
147 |
LIST *l; |
148 |
int d; |
149 |
{ |
150 |
LIST *list; |
151 |
|
152 |
list = new_list(); |
153 |
SET_LIST_DATA(list,d); |
154 |
SET_LIST_NEXT(list,l); |
155 |
return(list); |
156 |
} |
157 |
/* Returns data element d at the top of the list- returns pointer |
158 |
to new top of list |
159 |
*/ |
160 |
int |
161 |
pop_list(l) |
162 |
LIST **l; |
163 |
{ |
164 |
LIST *p; |
165 |
int d; |
166 |
|
167 |
if(!l) |
168 |
return(NULL); |
169 |
if(!(p = *l)) |
170 |
return(NULL); |
171 |
|
172 |
d = LIST_DATA(p); |
173 |
|
174 |
*l = LIST_NEXT(p); |
175 |
|
176 |
LIST_NEXT(p) =NULL; |
177 |
free_list(p); |
178 |
|
179 |
return(d); |
180 |
} |
181 |
|
182 |
/* Search through list for data element "d", and remove from |
183 |
* list: Returns TRUE if found, false otherwise |
184 |
*/ |
185 |
int |
186 |
remove_from_list(d,list) |
187 |
int d; |
188 |
LIST **list; |
189 |
{ |
190 |
LIST *l,*prev; |
191 |
|
192 |
l = *list; |
193 |
prev = NULL; |
194 |
|
195 |
while(l) |
196 |
{ |
197 |
if(LIST_DATA(l) == d) |
198 |
{ |
199 |
if(l == *list) |
200 |
*list = LIST_NEXT(*list); |
201 |
else |
202 |
SET_LIST_NEXT(prev,LIST_NEXT(l)); |
203 |
|
204 |
SET_LIST_NEXT(l,NULL); |
205 |
free_list(l); |
206 |
return(TRUE); |
207 |
} |
208 |
prev = l; |
209 |
l = LIST_NEXT(l); |
210 |
} |
211 |
return(FALSE); |
212 |
} |
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|