i am a beginner in C so i haven't covered hash tables yet...
here is what i wrote so far
Code:
#include <stdlib.h>
#include <stdio.h>
typedef struct tree { char thisnode[10]; 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[30]; Tree *tree = NULL;
do {
scanf( " %s", s);
if (strcmp(s, ".") != 0){
tree = insert (tree, s);
}
} while (strcmp(s, ".") != 0);
do{
printf("Type a name please:");
scanf(" %s",s);
if (strcmp(s,".") != 0){
printf("%s\n", search (tree,s ));
}
}while (strcmp(s, ".") != 0);
}