/*---------------------------------------------------------------------- File : scl_test.c Contents: test of operations on singly connected lists Author : Christian Borgelt History : 23.02.1998 file created 02.03.1998 adapted to generalized functions 03.06.1998 minor improvements ----------------------------------------------------------------------*/ #include #include #include #include "sclists.h" /*---------------------------------------------------------------------- Auxiliary Functions ----------------------------------------------------------------------*/ static void fltprt (void *call, void *client) { /* --- print a list element */ printf("%g\n", *(float*)call);/* print a floating point number */ } /* fltprt() */ /*------------------------------------------------------------------*/ static int fltcmp (const void *p1, const void *p2, void *client) { /* --- compare two list elements */ if (*(const float*)p1 > *(const float*)p2) return 1; if (*(const float*)p1 < *(const float*)p2) return -1; return 0; /* return the sign of *p1 - *p2 */ } /* fltcmp() */ /*--------------------------------------------------------------------*/ static int fltge (const void *p1, const void *p2, void *client) { /* --- find first greater or equal */ return (*(const float*)p1 > *(const float*)p2) ? -1 : 0; } /* fltge() */ /*---------------------------------------------------------------------- Main Functions ----------------------------------------------------------------------*/ int main (int argc, char *argv[]) { /* --- main function */ SCLIST *list; /* created singly connected list */ float *p; /* pointer to list element data */ float x; /* number to insert */ int cmd; /* command */ printf("%s --- test singly connected lists\n", argv[0]); printf("type l for a list of commands\n"); list = scl_create((SCL_DELFN*)0); if (!list) { printf("%s: not enough memory\n", argv[0]); return -1; } /* create a singly connected list */ while (1) { /* command evaluation loop */ printf("> "); fflush(stdout); do { cmd = getchar(); /* get next command */ } while (strchr(" \t\n", cmd) != NULL); switch (cmd) { /* evaluate command */ case 'i': case 'j': /* -- insert element */ if (scanf(" %f", &x) != 1) break; /* get number to insert */ if (cmd == 'j') { /* if to insert sorted */ scl_reset(list); /* reset cursor */ scl_search(list, &x, fltge, NULL); } /* search proper position */ p = (float*)scl_insert(list, sizeof(float)); if (!p) printf("not enough memory\n"); else *p = x; /* insert a list element */ break; /* and set number */ case 'd': /* --- remove element */ scl_remove(list, 0); break; case 'e': /* --- remove all elements */ scl_remove(list, 1); break; case 'r': /* --- reset cursor */ scl_reset(list); break; case 'n': /* --- go to next element */ scl_get(list, 1); break; case 'm': /* --- move to element */ if (scanf(" %f", &x) != 1) break; /* get number to move to */ scl_search(list, &x, fltcmp, NULL); break; /* search for next occurence */ case 'p': /* --- print element */ p = scl_get(list, 0); if (p) printf("%g\n", *p); break; case 's': /* --- sort the list */ scl_sort(list, fltcmp, NULL); break; case 'a': /* --- print all elements */ scl_apply(list, fltprt, NULL); printf("%d element(s)\n", scl_len(list)); break; case 'l': /* --- print command list */ printf("commands:\n"); printf("i x -- insert number x at current position\n"); printf("j x -- insert number x sorted\n"); printf("d -- delete element at current position\n"); printf("e -- empty the list (delete all elements)\n"); printf("r -- reset the current position\n"); printf("n -- go to the next element\n"); printf("m x -- move to the element with value x\n"); printf("s -- sort the list (ascending)\n"); printf("p -- print the current element\n"); printf("a -- print all elements in the list\n"); printf("l -- print this list of commands\n"); printf("q -- quit program\n"); break; /* print list of commands */ case 'q': /* quit program */ scl_delete(list); /* delete the list */ return 0; /* and return `ok' */ default : /* ignore everything else */ printf("unknown command --- type l for a list\n"); break; } } /* while (1) */ } /* main() */