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