/*---------------------------------------------------------------------- File : dcl_test.c Contents: test of operations on doubly connected lists Author : Christian Borgelt History : 03.03.1998 file created from file scl_test.c 03.06.1998 minor improvements ----------------------------------------------------------------------*/ #include #include #include #include "dclists.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 */ DCLIST *list; /* created doubly connected list */ float *p; /* pointer to list element data */ float x; /* number to insert */ int cmd; /* command */ printf("%s --- test doubly connected lists\n", argv[0]); printf("type l for a list of commands\n"); list = dcl_create((DCL_DELFN*)0); if (!list) { printf("%s: not enough memory\n", argv[0]); return -1; } /* create a doubly 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, */ dcl_reset(list, DCL_FRONT); /* reset cursor */ dcl_search(list, &x, DCL_FORWARD, fltge, NULL); } /* search proper position */ p = (float*)dcl_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 */ dcl_remove(list, 0); break; case 'e': /* --- remove all elements */ dcl_remove(list, 1); break; case 'h': /* --- go to the head */ dcl_reset(list, DCL_FRONT); break; case 't': /* --- go to the tail */ dcl_reset(list, DCL_BACK); break; case 'f': /* --- go forward */ dcl_get(list, DCL_FORWARD); break; case 'b': /* --- go backward */ dcl_get(list, DCL_BACKWARD); break; case 'm': /* --- move to element */ if (scanf(" %f", &x) != 1) break; /* get number to move to */ dcl_search(list, &x, DCL_FORWARD, fltcmp, NULL); break; /* and search proper position */ case 'p': /* --- print element */ p = dcl_get(list, DCL_STAY); if (p) printf("%g\n", *p); break; case 's': /* --- sort the list */ dcl_sort(list, fltcmp, NULL); break; case 'a': /* --- print all elements */ dcl_apply(list, fltprt, NULL); printf("%d element(s)\n", dcl_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("h -- go to the head (front) of the list\n"); printf("t -- go to the tail (back) of the list\n"); printf("f -- go forward (to the next element)\n"); printf("b -- go backward (to the preceding 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 */ dcl_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() */