00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include <stdio.h>
00016 #include <stdlib.h>
00017 #include <string.h>
00018
00019 #include "sflsq.h"
00020
00021
00022
00023
00024 static void * s_malloc (int n)
00025 {
00026 void *p=0;
00027 if( n > 0 )p = (void*) malloc( n );
00028 return p;
00029 }
00030
00031
00032
00033
00034 static void s_free (void *p)
00035 {
00036 if( p ) free( p );
00037 }
00038
00039
00040
00041
00042 void sflist_init ( SF_LIST * s)
00043 {
00044 s->count=0;
00045 s->head = s->tail = s->cur = 0;
00046 }
00047
00048
00049
00050
00051 SF_LIST * sflist_new()
00052 {
00053 SF_LIST * s;
00054 s = (SF_LIST*)s_malloc( sizeof(SF_LIST) );
00055 if( s )sflist_init( s );
00056 return s;
00057 }
00058
00059 SF_STACK * sfstack_new()
00060 {
00061 return (SF_STACK*)sflist_new();
00062 }
00063
00064 SF_QUEUE * sfqueue_new()
00065 {
00066 return (SF_QUEUE*)sflist_new();
00067 }
00068
00069
00070
00071
00072
00073
00074
00075
00076 int
00077 sflist_add_head ( SF_LIST* s, NODE_DATA ndata )
00078 {
00079 SF_LNODE * q;
00080 if (!s->head)
00081 {
00082 q = s->tail = s->head = (SF_LNODE *) s_malloc (sizeof (SF_LNODE));
00083 if(!q)return -1;
00084 q->ndata = (NODE_DATA)ndata;
00085 q->next = 0;
00086 q->prev = 0;
00087 }
00088 else
00089 {
00090 q = (SF_LNODE *) s_malloc (sizeof (SF_LNODE));
00091 if(!q)return -1;
00092 q->ndata = ndata;
00093 q->next = s->head;
00094 q->prev = 0;
00095 s->head->prev = q;
00096 s->head = q;
00097
00098 }
00099 s->count++;
00100
00101 return 0;
00102 }
00103
00104
00105
00106
00107 int
00108 sflist_add_tail ( SF_LIST* s, NODE_DATA ndata )
00109 {
00110 SF_LNODE * q;
00111 if (!s->head)
00112 {
00113 q = s->tail = s->head = (SF_LNODE *) s_malloc (sizeof (SF_LNODE));
00114 if(!q)return -1;
00115 q->ndata = (NODE_DATA)ndata;
00116 q->next = 0;
00117 q->prev = 0;
00118 }
00119 else
00120 {
00121 q = (SF_LNODE *) s_malloc (sizeof (SF_LNODE));
00122 if(!q)return -1;
00123 q->ndata = ndata;
00124 q->next = 0;
00125 q->prev = s->tail;
00126 s->tail->next = q;
00127 s->tail = q;
00128 }
00129 s->count++;
00130
00131 return 0;
00132 }
00133
00134
00135
00136 int sflist_add_before ( SF_LIST* s, SF_LNODE * lnode, NODE_DATA ndata )
00137 {
00138 SF_LNODE * q;
00139
00140 if( !lnode )
00141 return 0;
00142
00143
00144 if( s->head == lnode )
00145 {
00146 return sflist_add_head ( s, ndata );
00147 }
00148 else
00149 {
00150 q = (SF_LNODE *) s_malloc ( sizeof (SF_LNODE) );
00151 if( !q )
00152 {
00153 return -1;
00154 }
00155 q->ndata = (NODE_DATA)ndata;
00156
00157 q->next = lnode;
00158 q->prev = lnode->prev;
00159 lnode->prev->next = q;
00160 lnode->prev = q;
00161 }
00162 s->count++;
00163
00164 return 0;
00165 }
00166
00167
00168
00169 int sfqueue_add(SF_QUEUE * s, NODE_DATA ndata )
00170 {
00171 return sflist_add_tail ( s, ndata );
00172 }
00173
00174 int sfstack_add( SF_STACK* s, NODE_DATA ndata )
00175 {
00176 return sflist_add_tail ( s, ndata );
00177 }
00178
00179
00180
00181
00182 NODE_DATA sflist_first( SF_LIST * s )
00183 {
00184 s->cur = s->head;
00185 if( s->cur )
00186 return s->cur->ndata;
00187 return 0;
00188 }
00189 NODE_DATA sflist_next( SF_LIST * s )
00190 {
00191 if( s->cur )
00192 {
00193 s->cur = s->cur->next;
00194 if( s->cur )
00195 return s->cur->ndata;
00196 }
00197 return 0;
00198 }
00199 NODE_DATA sflist_prev( SF_LIST * s )
00200 {
00201 if( s->cur )
00202 {
00203 s->cur = s->cur->prev;
00204 if( s->cur )
00205 return s->cur->ndata;
00206 }
00207 return 0;
00208 }
00209
00210
00211
00212 SF_LNODE * sflist_first_node( SF_LIST * s )
00213 {
00214 s->cur = s->head;
00215 if( s->cur )
00216 return s->cur;
00217 return 0;
00218 }
00219 SF_LNODE * sflist_next_node( SF_LIST * s )
00220 {
00221 if( s->cur )
00222 {
00223 s->cur = s->cur->next;
00224 if( s->cur )
00225 return s->cur;
00226 }
00227 return 0;
00228 }
00229
00230
00231
00232
00233 NODE_DATA sflist_remove_head (SF_LIST * s)
00234 {
00235 NODE_DATA ndata = 0;
00236 SF_QNODE * q;
00237 if (s->head)
00238 {
00239 q = s->head;
00240 ndata = q->ndata;
00241 s->head = s->head->next;
00242 s->count--;
00243 if (!s->head)
00244 {
00245 s->tail = 0;
00246 s->count = 0;
00247 }
00248 s_free( q );
00249 }
00250 return (NODE_DATA)ndata;
00251 }
00252
00253
00254
00255
00256 NODE_DATA sflist_remove_tail (SF_LIST * s)
00257 {
00258 NODE_DATA ndata = 0;
00259 SF_QNODE * q;
00260 if (s->tail)
00261 {
00262 q = s->tail;
00263
00264 ndata = q->ndata;
00265 s->count--;
00266 s->tail = q->prev;
00267 if (!s->tail)
00268 {
00269 s->tail = 0;
00270 s->head = 0;
00271 s->count = 0;
00272 }
00273 else
00274 {
00275 q->prev->next = 0;
00276 }
00277 s_free (q);
00278 }
00279 return (NODE_DATA)ndata;
00280 }
00281
00282
00283
00284
00285
00286 NODE_DATA sflist_remove_current (SF_LIST * s)
00287 {
00288 NODE_DATA ndata = NULL;
00289 SF_LNODE *l;
00290
00291 l = s->cur;
00292
00293 if(l)
00294 {
00295 ndata = l->ndata;
00296
00297 if(l->prev)
00298 {
00299 l->prev->next = l->next;
00300 s->cur = l->prev;
00301 }
00302 else
00303 {
00304 s->head = l->next;
00305 s->cur = l->next;
00306 }
00307
00308 if(l->next)
00309 l->next->prev = l->prev;
00310 else
00311 s->tail = l->prev;
00312
00313 s->count--;
00314 s_free(l);
00315 return (NODE_DATA)ndata;
00316 }
00317
00318 return NULL;
00319 }
00320
00321
00322
00323
00324
00325 NODE_DATA sfqueue_remove (SF_QUEUE * s)
00326 {
00327 return (NODE_DATA)sflist_remove_head( s );
00328 }
00329
00330
00331
00332
00333 NODE_DATA sfstack_remove (SF_QUEUE * s)
00334 {
00335 return (NODE_DATA)sflist_remove_tail( s );
00336 }
00337
00338
00339
00340
00341 int sfqueue_count (SF_QUEUE * s)
00342 {
00343 if(!s)return 0;
00344 return s->count;
00345 }
00346 int sflist_count ( SF_LIST* s)
00347 {
00348 if(!s)return 0;
00349 return s->count;
00350 }
00351 int sfstack_count ( SF_STACK * s)
00352 {
00353 if(!s)return 0;
00354 return s->count;
00355 }
00356
00357
00358
00359
00360
00361 void sflist_free_all( SF_LIST * s, void (*nfree)(void*) )
00362 {
00363 void * p;
00364 while( sflist_count(s) )
00365 {
00366 p = sflist_remove_head (s);
00367 if(p)nfree(p);
00368 }
00369 }
00370 void sfqueue_free_all(SF_QUEUE * s,void (*nfree)(void*) )
00371 {
00372 sflist_free_all( s, nfree );
00373 }
00374 void sfstack_free_all(SF_STACK * s,void (*nfree)(void*) )
00375 {
00376 sflist_free_all( s, nfree );
00377 }
00378
00379
00380
00381
00382
00383
00384 void sflist_free (SF_LIST * s)
00385 {
00386 while( sflist_count(s) )
00387 {
00388 sflist_remove_head (s);
00389 }
00390 }
00391 void sfqueue_free (SF_QUEUE * s)
00392 {
00393 sflist_free ( s );
00394 }
00395 void sfstack_free (SF_STACK * s)
00396 {
00397 sflist_free ( s );
00398 }
00399
00400
00401
00402
00403 int sfistack_init( SF_ISTACK * s, unsigned * a, int n )
00404 {
00405 s->imalloc=0;
00406 if( a ) s->stack = a;
00407 else
00408 {
00409 s->stack = (unsigned*) malloc( n * sizeof(unsigned) );
00410 s->imalloc=1;
00411 }
00412 if( !s->stack ) return -1;
00413 s->nstack= n;
00414 s->n =0;
00415 return 0;
00416 }
00417 int sfistack_push( SF_ISTACK *s, unsigned value)
00418 {
00419 if( s->n < s->nstack )
00420 {
00421 s->stack[s->n++] = value;
00422 return 0;
00423 }
00424 return -1;
00425 }
00426 int sfistack_pop( SF_ISTACK *s, unsigned * value)
00427 {
00428 if( s->n > 0 )
00429 {
00430 s->n--;
00431 *value = s->stack[s->n];
00432 return 0;
00433 }
00434 return -1;
00435 }
00436
00437
00438
00439
00440 int sfpstack_init( SF_PSTACK * s, void ** a, int n )
00441 {
00442 s->imalloc=0;
00443 if( a ) s->stack = a;
00444 else
00445 {
00446 s->stack = (void**) malloc( n * sizeof(void*) );
00447 s->imalloc=1;
00448 }
00449
00450 if( !s->stack ) return -1;
00451 s->nstack= n;
00452 s->n =0;
00453 return 0;
00454 }
00455 int sfpstack_push( SF_PSTACK *s, void * value)
00456 {
00457 if( s->n < s->nstack )
00458 {
00459 s->stack[s->n++] = value;
00460 return 0;
00461 }
00462 return -1;
00463 }
00464 int sfpstack_pop( SF_PSTACK *s, void ** value)
00465 {
00466 if( s->n > 0 )
00467 {
00468 s->n--;
00469 *value = s->stack[s->n];
00470 return 0;
00471 }
00472 return -1;
00473 }
00474