i need to create a telephone book using a tree. so for example i type in bob 0723393434
bill 032423423
. (input is stopped on reading a '.')
i then have to prompt the user to type in a name, for example bob and get the program to respond with the telephone number , e.g. 072339344.
example:
bob 3234343223
bill 2210382131.
Type a name please bob
3234343223
.
this is my code so far:it compiles but the problem is that instead of finding the name and printing the telephone number it prints out just the name, how do i get it to print out the telephone number instead of the name? also how do i get it to not be case sensitive?Code:#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct tree { char thisnode[20]; struct tree *left; struct tree *right; }Tree; Tree *makenode( char *in, Tree *l, Tree *r) { Tree *t=malloc(sizeof(Tree)); t->left =l; t->right =r; strncpy(t->thisnode, in, 9); return t; } Tree *insert( Tree *root, char *what) { if(root==NULL) {root=makenode(what, NULL,NULL);} else if(strcmp(what, root->thisnode)<0) {root->left =insert(root->left,what);} else{root->right =insert(root->right, what); } return root; } char *search(Tree *root, char *what) { if(root==NULL){return "Not found";} else if(strcmp(what,root->thisnode)==0) {return root->thisnode;} else if(strcmp(what,root->thisnode)<0) {return search(root->left,what);} else{return search(root->right,what);} } int main(void) { char s[20]; Tree *tree =NULL; printf("Please Input some names and telephone numbers:\n"); do{ scanf("%s",s); if(strcmp(s,".")!=0) {tree=insert(tree, s);} } while(strcmp(s,".")!=0); printf("Please enter in a name:\n"); do{ scanf("%s",s); if(strcmp(s,".")!=0) {printf("%s\n", search(tree,s));} }while(strcmp(s,".")!=0); return 0; }



LinkBack URL
About LinkBacks


