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(!a) |
54 |
return(b); |
55 |
|
56 |
if(!b) |
57 |
return(a); |
58 |
|
59 |
l = b; |
60 |
while(LIST_NEXT(l) && LIST_NEXT(l) != b) |
61 |
l = LIST_NEXT(l); |
62 |
|
63 |
SET_LIST_NEXT(l,a); |
64 |
|
65 |
return(b); |
66 |
} |
67 |
|
68 |
/* Adds data to the end of a circular list. If set, "end" |
69 |
* is a pointer to the last element in the list |
70 |
*/ |
71 |
LIST |
72 |
*add_data_to_circular_list(l,end,d) |
73 |
LIST *l,**end; |
74 |
int d; |
75 |
{ |
76 |
LIST *list; |
77 |
|
78 |
list = new_list(); |
79 |
SET_LIST_DATA(list,d); |
80 |
|
81 |
if(!l) |
82 |
l = list; |
83 |
else |
84 |
{ |
85 |
if(*end) |
86 |
SET_LIST_NEXT(*end,list); |
87 |
else |
88 |
l = append_list(list,l); |
89 |
} |
90 |
|
91 |
*end = list; |
92 |
SET_LIST_NEXT(list,l); |
93 |
return(l); |
94 |
} |
95 |
|
96 |
/* Pushes data element d at the top of the list- returns pointer |
97 |
to new top of list |
98 |
*/ |
99 |
LIST |
100 |
*push_data(l,d) |
101 |
LIST *l; |
102 |
int d; |
103 |
{ |
104 |
LIST * list; |
105 |
|
106 |
list = new_list(); |
107 |
SET_LIST_DATA(list,d); |
108 |
SET_LIST_NEXT(list,l); |
109 |
return(list); |
110 |
} |
111 |
|
112 |
/* frees the list */ |
113 |
LIST |
114 |
*free_list(l) |
115 |
LIST * l; |
116 |
{ |
117 |
if(!l) |
118 |
return(NULL); |
119 |
|
120 |
free_lists = append_list(free_lists,l); |
121 |
|
122 |
return(NULL); |
123 |
} |
124 |
|
125 |
/* Returns data element d at the top of the list- returns pointer |
126 |
to new top of list |
127 |
*/ |
128 |
int |
129 |
pop_list(l) |
130 |
LIST **l; |
131 |
{ |
132 |
LIST *p; |
133 |
int d; |
134 |
|
135 |
if(!l) |
136 |
return(NULL); |
137 |
if(!(p = *l)) |
138 |
return(NULL); |
139 |
|
140 |
d = LIST_DATA(p); |
141 |
|
142 |
*l = LIST_NEXT(p); |
143 |
|
144 |
LIST_NEXT(p) =NULL; |
145 |
free_list(p); |
146 |
|
147 |
return(d); |
148 |
} |
149 |
|
150 |
/* Search through list for data element "d", and remove from |
151 |
* list: Returns TRUE if found, false otherwise |
152 |
*/ |
153 |
int |
154 |
remove_from_list(d,list) |
155 |
int d; |
156 |
LIST **list; |
157 |
{ |
158 |
LIST *l,*prev; |
159 |
|
160 |
l = *list; |
161 |
prev = NULL; |
162 |
|
163 |
while(l) |
164 |
{ |
165 |
if(LIST_DATA(l) == d) |
166 |
{ |
167 |
if(l == *list) |
168 |
*list = LIST_NEXT(*list); |
169 |
else |
170 |
SET_LIST_NEXT(prev,LIST_NEXT(l)); |
171 |
|
172 |
SET_LIST_NEXT(l,NULL); |
173 |
free_list(l); |
174 |
return(TRUE); |
175 |
} |
176 |
prev = l; |
177 |
l = LIST_NEXT(l); |
178 |
} |
179 |
return(FALSE); |
180 |
} |
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|