00001
00079 #include "oldlist.h"
00080 #include "structures.h"
00081 #include <stdio.h>
00082 #if MAC_OR_DOS
00083 #include <stdlib.h>
00084 #else
00085 #include "freelist.h"
00086 #endif
00087
00088
00089
00090
00091 #define add_on(l,x) l = push (l,first (x))
00092 #define next_one(l) l = rest (l)
00093
00094
00095
00096
00102 int count(LIST var_list) {
00103 int temp = 0;
00104
00105 iterate (var_list) temp += 1;
00106 return (temp);
00107 }
00108
00109
00123 LIST delete_d(LIST list, void *key, int_compare is_equal) {
00124 LIST result = NIL;
00125 LIST last_one = NIL;
00126
00127 if (is_equal == NULL)
00128 is_equal = is_same;
00129
00130 while (list != NIL) {
00131 if (!(*is_equal) (first (list), key)) {
00132 if (last_one == NIL) {
00133 last_one = list;
00134 list = rest (list);
00135 result = last_one;
00136 set_rest(last_one, NIL);
00137 }
00138 else {
00139 set_rest(last_one, list);
00140 last_one = list;
00141 list = rest (list);
00142 set_rest(last_one, NIL);
00143 }
00144 }
00145 else {
00146 list = pop (list);
00147 }
00148 }
00149 return (result);
00150 }
00151
00152
00156 LIST destroy(LIST list) {
00157 LIST next;
00158
00159 while (list != NIL) {
00160 next = rest (list);
00161 free_cell(list);
00162 list = next;
00163 }
00164 return (NIL);
00165 }
00166
00167
00171 void destroy_nodes(LIST list, void_dest destructor) {
00172 if (destructor == NULL)
00173 destructor = memfree;
00174
00175 while (list != NIL) {
00176 (*destructor) (first (list));
00177 list = pop (list);
00178 }
00179 }
00180
00181
00186 void insert(LIST list, void *node) {
00187 LIST element;
00188
00189 if (list != NIL) {
00190 element = push (NIL, node);
00191 set_rest (element, rest (list));
00192 set_rest(list, element);
00193 node = first (list);
00194 list->node = first (rest (list));
00195 list->next->node = (LIST) node;
00196 }
00197 }
00198
00199
00204 int is_same_node(void *item1, void *item2) {
00205 return (item1 == item2);
00206 }
00207
00208
00213 int is_same(void *item1, void *item2) {
00214 return (!strcmp ((char *) item1, (char *) item2));
00215 }
00216
00217
00224 LIST join(LIST list1, LIST list2) {
00225 if (list1 == NIL)
00226 return (list2);
00227 set_rest (last (list1), list2);
00228 return (list1);
00229 }
00230
00231
00235 LIST last(LIST var_list) {
00236 while (rest (var_list) != NIL)
00237 var_list = rest (var_list);
00238 return (var_list);
00239 }
00240
00241
00245 void *nth_cell(LIST var_list, int item_num) {
00246 int x = 0;
00247 iterate(var_list) {
00248 if (x++ == item_num)
00249 return (var_list);
00250 }
00251 return (var_list);
00252 }
00253
00254
00259 LIST pop(LIST list) {
00260 LIST temp;
00261
00262 temp = rest (list);
00263
00264 if (list != NIL) {
00265 free_cell(list);
00266 }
00267 return (temp);
00268 }
00269
00270
00277 LIST push(LIST list, void *element) {
00278 LIST t;
00279
00280 t = new_cell ();
00281 t->node = (LIST) element;
00282 set_rest(t, list);
00283 return (t);
00284 }
00285
00286
00292 LIST push_last(LIST list, void *item) {
00293 LIST t;
00294
00295 if (list != NIL) {
00296 t = last (list);
00297 t->next = push (NIL, item);
00298 return (list);
00299 }
00300 else
00301 return (push (NIL, item));
00302 }
00303
00304
00310 LIST reverse(LIST list) {
00311 LIST newlist = NIL;
00312
00313 iterate (list) copy_first (list, newlist);
00314 return (newlist);
00315 }
00316
00317
00323 LIST reverse_d(LIST list) {
00324 LIST result = reverse (list);
00325 destroy(list);
00326 return (result);
00327 }
00328
00329
00335 LIST s_adjoin(LIST var_list, void *variable, int_compare compare) {
00336 LIST l;
00337 int result;
00338
00339 if (compare == NULL)
00340 compare = (int_compare) strcmp;
00341
00342 l = var_list;
00343 iterate(l) {
00344 result = (*compare) (variable, first (l));
00345 if (result == 0)
00346 return (var_list);
00347 else if (result < 0) {
00348 insert(l, variable);
00349 return (var_list);
00350 }
00351 }
00352 return (push_last (var_list, variable));
00353 }
00354
00355
00364 LIST search(LIST list, void *key, int_compare is_equal) {
00365 if (is_equal == NULL)
00366 is_equal = is_same;
00367
00368 iterate (list) if ((*is_equal) (first (list), key))
00369 return (list);
00370 return (NIL);
00371 }