i now want to be able to input a name and a telephone number , and then the same name with a different telephone number and get the program to print out both numbers:
e.g. tom 34233423
matt 3424334
tom 32343322
.
please type a name: tom
34233423
32343322
i was thinking about creating a lookup table or changing my search function somehow(however i couldnt keep looping through it as you would eventually get to NOT FOUND) , does anyone have any ideas of how to do this?
here is my code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct tree
{
char name[20];
char phone[20];
struct tree *left;
struct tree *right;
}Tree;
Tree *makenode( char *in, char *in2, Tree *l, Tree *r)
{
Tree *t=malloc(sizeof(Tree));
t->left =l;
t->right =r;
strncpy(t->name,in,19);
strncpy(t->phone,in2,19);
return t;
}
Tree *insert( Tree *root, char *what, char *what2)
{
if(root==NULL)
{root=makenode(what,what2,NULL,NULL);}
else if(strcmp(what, root->name)<0)
{root->left =insert(root->left,what, what2);}
else{root->right =insert(root->right,what,what2);
}
return root;
}
char *search(Tree *root, char *what)
{
if(root==NULL){return "NOT FOUND";}
else if(strcmp(what,root->name)==0) {return root->phone;}
else if(strcmp(what,root->name)<0) {return search(root->left,what);}
else{return search(root->right,what);}
}
int main(void)
{
int i;
char s[20];
char b[20];
Tree *tree =NULL;
do{
scanf("%s",s);
if(strcmp(s,".")==0)
{break;}
scanf("%s",b);
for(i=0;i<20;i++)
{s[i]=toupper(s[i]);}
if(strcmp(s,".")!=0);
{tree=insert(tree, s,b);}
}
while(strcmp(s,".")!=0);
do{
printf("Type a name name please ");
scanf("%s",s);
for(i=0;i<20;i++)
{s[i]=toupper(s[i]);}
if(strcmp(s,".")!=0)
{printf("%s\n", search(tree,s));}
}while(strcmp(s,".")!=0);
return 0;
}
thanks