Reputation: 305
I have 2 files: dictionary and sample.
void print_table (Table);
void print_stats (Table);
Upvotes: 1
Views: 960
Reputation: 5467
Several problems:
- You are always returning false from Fixed by userfind
- You are always adding a print out of 'not found' whenever the root of the search isn't a match.
else if(key<head->element) { printf("not found"); return search(key, head->left);
And the bigger two problems
C
language and key
and head->element
are both const char* or char*; you can't use the ==
operator on them. That will only work if the pointers point to the same address. You want to use strcmp
instead.key
to head->left
; you probably mean to compare to head->left->element
. Otherwise you are comparing a char* to a node*. But again strcmp
simplifies all of this, as you actually don't need to and shouldn't do this check here; particularly as head->left might itself be NULL
.As in below:
struct node *search( Key_Type key, struct node *head )
{
if ( head != NULL)
{
//note change to strcmp
int res = strcmp( key, head->element );
if ( !res )
{
printf("found");
return head;
}
else if( res < 0 ) //note change to strcmp.
{
printf( "not found" );
return search( key, head->left );
}
else
{
printf( "found3" );
return search( key, head->right );
}
}
else
return FALSE;
}
Upvotes: 1