Beispiel #1
0
/* insert vcard into bst */ 
int bst_insert(bst *t, vcard *c)
{
    if(t == NULL) { 
	fprintf(stderr, "Tree is null.\n"); 
	exit(1); 
    } 
    int cmp = strcmp(c -> cnet, t -> c -> cnet); 
    if(cmp == 0) 
	return 0; 
    
    if(cmp < 0) { 
	if(t -> lsub == NULL) { 
	    t -> lsub = bst_singleton(c); 
	    return 1; 
	} 
	return bst_insert(t -> lsub, c); 
    } 

    if(t -> rsub == NULL) { 
	    t -> rsub = bst_singleton(c); 
	    return 1; 
    } 
    
    return bst_insert(t -> rsub, c);     
}
Beispiel #2
0
Datei: bst.c Projekt: gaibo/C
int bst_insert_helper(bst* t, vcard* c) {
    // short-circuit if cnet is already there
    if (!strcmp(c->cnet, t->c->cnet)) {
        return 0;
    }
    
    // recursive step
    if (strcmp(c->cnet, t->c->cnet) < 0) {
        if (t->lsub == NULL) {
            t->lsub = bst_singleton(c);
        } else {
            bst_insert_helper(t->lsub, c);
        }
    } else if (strcmp(c->cnet, t->c->cnet) > 0) {
        if (t->rsub == NULL) {
            t->rsub = bst_singleton(c);
        } else {
            bst_insert_helper(t->rsub, c);
        }
    }
    return 1;
}
Beispiel #3
0
Datei: main.c Projekt: gaibo/C
bst *addr_book_from_file(char *infile)
{
  vcard *c;
  char buf0[BUF_SZ], buf1[BUF_SZ], buf2[BUF_SZ], buf3[BUF_SZ], buf4[BUF_SZ];
  bst *address_book = NULL;
  FILE *f = fopen(infile,"r");
  if (f==NULL) {
    fprintf(stdout,"File not found: %s.\n", infile);
    return NULL;
  }
  while (!feof(f)) {
    fscanf(f,"%s\t%s\t%s\t%s\t%s\n",buf0,buf1,buf2,buf3,buf4);    
    c = vcard_new(buf0,buf1,buf2,buf3,buf4);
    if (address_book)
      bst_insert(address_book, c);
    else
      address_book = bst_singleton(c);
  }
  fclose(f);
  return address_book;
}