home *** CD-ROM | disk | FTP | other *** search
- /*
- From moraes@csri.toronto.edu Mon Apr 3 15:16:23 1989
- From: Mark Moraes <moraes@csri.toronto.edu>
- To: daveb@rtech.uucp
- Subject: small fix for sptree
- Date: Mon, 3 Apr 89 02:18:23 EDT
-
- Hi.
-
- I enclose a diff to fix a problem in spaux.c, without which it dumps
- code when deleting an item.
-
- I also enclose a little example program I used to test some of the
- tree functions. (not the queue ones though) Do you have a better test?
-
- [ nope. I'll use/hack the one he gave me -dB ]
- */
-
- #include <stdio.h>
- #define MAXSTR 512
-
- #include "sptree.h"
-
- char *
- strsave( s )
- char *s;
- {
- char *emalloc();
- char *strcpy();
- return ( strcpy( emalloc( strlen(s) + 1 ), s ) );
- }
-
- char *
- emalloc(n)
- int n;
- {
- char *malloc();
- char *p = malloc( n );
- if( p )
- return( p );
-
- fprintf(stderr, "emalloc: can't allocate %d bytes\n", n );
- perror("possible cause");
- exit( 1 );
- /*NOTREACHED*/
- }
-
-
- #define prompt() if(prompt_p) printf("testsp>"); else ;
-
- main()
- {
- char buf[MAXSTR];
- char s1[8], s2[MAXSTR], s3[MAXSTR];
- SPBLK *result;
- int prompt_p;
- SPTREE *sp;
- int prnode();
-
- sp = spinit();
- prompt_p = isatty(fileno(stdin));
- prompt();
- while(fgets(buf, sizeof(buf), stdin) != NULL) {
- sscanf(buf, " %s %s", s1, s2);
- switch (*s1) {
- case 'a':
- sscanf(buf, " %*s %*s %s", s3);
- spinstall(strsave(s2), strsave(s3), NULL, sp);
- break;
- case 's':
- if ((result = splookup(s2, sp)) != NULL)
- printf("found %s\n", result->data);
- else
- printf("no match\n");
- break;
- case 'd':
- if ((result = splookup(s2, sp)) != NULL)
- spdelete(result, sp);
- else
- printf("no match\n");
- break;
- case 'l':
- spscan(prnode, NULL, sp);
- break;
- case 'p':
- spshow(sp);
- break;
- case 'q':
- exit( 0 );
- break;
- default:
- printf(
- "'a string1 string2' inserts datum (string2) keyed by string1\n");
- printf(
- "'d string1' deletes the datum associated with string1 if any\n");
- printf(
- "'s string1' prints the datum associated with string1 if any\n");
- printf(
- "'l' lists the elements of the tree in spscan() order\n");
- printf(
- "'p' prints the shape and contents of the tree with spshow()\n");
- break;
- }
- prompt();
- }
- printf("\n");
- #ifdef PRINT
- printf("Stats = %s\n", spstats(sp));
- #endif
- }
-
- int
- prnode(spblk)
- SPBLK *spblk;
- {
- printf("Key = \"%s\", Data = \"%s\"\n", spblk->key, spblk->data);
- }
-
-