|
とりあえずさらっと見ただけなのでまだバグがあるかもしれません。
リスト構造のデータ及びポインタと実体の扱い方が間違っていました。
こういう場合デバッグはHASH_SIZEを1にして正しくリストが構築されて
いるか確かめるのが正攻法です。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASH_SIZE 5001
#define BUF_SIZE 16
typedef struct cell{
int number ;
char name[BUF_SIZE] ;
struct cell *next ;
} data ;
data *hashtable[HASH_SIZE] ;
FILE *fp ;
void MakeHashTable(void) ;
void Chaining(data * ,unsigned int) ;
int Search_C(char *) ; // Chaining
unsigned int Hash(char *) ; // Hash Function
void PrintHash(void) ;
void Free(void) ;
int main(void)
{
char buf[BUF_SIZE];
int func;
MakeHashTable() ;
PrintHash() ;
printf("********** SEARCH (Chaining) **********\n") ;
printf("Input a word : ") ;
scanf("%s" , buf) ;
if((func = Search_C(buf)) != 0)
printf("Comparison Times : %d\n" , func) ;
else
printf("No such a word in Hash Table .\n") ;
/* release memory teritory */
Free() ;
fclose( fp ) ;
return 0;
}
void MakeHashTable(){
data *temp ; // temporary node for insert data to Hash Table
int i ;
// Initialize Hash Table
for(i = 0 ; i < HASH_SIZE ; i++)
hashtable[i] = NULL ;
// acquire the file-pointer
if((fp = fopen("data.txt" , "r")) == NULL){
printf("ERROR : Can't open the file .\n") ;
exit(1) ;
}
for(;;){
temp = (data *)malloc(sizeof(data)) ;
// make temporary node
if(fscanf(fp , "%d" , &temp->number) != EOF){
fscanf(fp , "%s" , temp->name) ;
temp->next = NULL ;
Chaining(temp , Hash( temp->name )) ;
free( temp );
} else {
free( temp );
break ;
}
}
}
void Chaining (data *temp , unsigned int f )
{
data *s ; // substitute for Hashtable[f]
if (hashtable[f] == NULL){
hashtable[f] = (data *) malloc (sizeof ( data ));
*hashtable[f] = *temp ;
} else {
s = hashtable[f];
while (s->next)
s = s->next;
s->next = (data *) malloc (sizeof(data));
*s->next = *temp ;
s->next->next = NULL;
}
}
unsigned int Hash(char *buf){
unsigned int value ;
for(value = 0 ; *buf != '\0'; buf++)
value = *buf + value * 11 ;
return value % HASH_SIZE ;
}
int Search_C(char *buf){
data *s ; // substitute for Hashtable[f]
int cmp = 1 ;
unsigned int f ;
f = Hash( buf ) ;
s = hashtable[f] ;
while(s){ // something exit in hashtable[f]
if(strcmp(s->name , buf) != 0){
cmp++ ;
s = s->next ;
} else return cmp ;
}
return 0 ;
}
void PrintHash(void){
data *s ; // substitute for hashtable[i]
int i ;
for(i = 0 ; i < HASH_SIZE ; i++){
printf("[%d]\t",i) ;
s = hashtable[i] ;
while(s){
printf("%4d %-16s\t -> ", s->number , s->name) ;
s = s->next ;
}
printf(" ( NULL )\n") ;
}
}
void Free(void){
int i ;
data *s, *t;
for(i = 0 ; i < HASH_SIZE ; i++){
s = hashtable[i];
while (s) {
t = s->next ;
free( s ) ;
s = t;
}
}
}
|