#include #include #include #define HASH_SIZE 100 struct PhoneNameList { int phone ; char name[ 20 ] ; struct PhoneNameList* next ; } ; struct PhoneNameList* hash[ HASH_SIZE ] ; struct PhoneNameList* cons( int ph , char* nm , struct PhoneNameList* nx ) { struct PhoneNameList* ans ; ans = (struct PhoneNameList*)malloc( sizeof( struct PhoneNameList ) ) ; if ( ans != NULL ) { ans->phone = ph ; strcpy( ans->name , nm ) ; ans->next = nx ; } return ans ; } int hash_func( int phone ) { return phone % HASH_SIZE ; } // 配列に電話番号と名前を保存 void entry( int phone , char* name ) { int idx = hash_func( phone ) ; hash[ idx ] = cons( phone , name , hash[ idx ] ) ; } // 電話番号から名前を調べる char* search( int phone ) { struct PhoneNameList* p ; int idx = hash_func( phone ) ; for( p = hash[ idx ] ; p != NULL ; p = p->next ) { if ( p->phone == phone ) return p->name ; } return NULL ; // 見つからなかった } int main() { char *ans ; entry( 272925 , "t-saitoh" ) ; entry( 621111 , "FukuiNCT" ) ; entry( 123425 , "FooBar" ) ; if ( (ans = search( 272925 )) != NULL ) printf( "%s\n" , ans ) ; if ( (ans = search( 123425 )) != NULL ) printf( "%s\n" , ans ) ; return 0 ; }