标签:
不会看书(su)啊~~
#include<cstdio> #include<cstring> #include<cstdlib> #define HASHSIZE 12 #define NULLKEY -32768 typedef struct { int *elem; int count; }HashTable; int m=0; int InitHashTable(HashTable *H){ int i; m = HASHSIZE; H->count = m; H->elem = (int *)malloc(sizeof(int)*m); for(i=0;i<m;i++) H->elem[i] = NULLKEY; return 1; } int Hash(int key){ return key%m; //除留余数法 } void InsertHash(HashTable *H,int key){ int addr = Hash(key); while(H->elem[addr] != NULLKEY){ addr = (addr+1) % m; } H->elem[addr] = key; } int SearchHash(HashTable H,int key,int *addr){ *addr = Hash(key); while(H.elem[*addr] != key){ //no *addr = (*addr+1) % m; //往下找 if(H.elem[*addr] == NULLKEY || *addr == Hash(key)){ //不是 || 回到起点 return 0; } } return 1; } int main(){ int a[] = {12,67,56,16,25,37,22,29,15,47,48,34}; int i; int key; int addr; HashTable H; InitHashTable(&H); for(i=0;i<12;i++){ InsertHash(&H,a[i]); } while(~scanf("%d",&key)){ if(SearchHash(H,key,&addr)) printf("%d\n",addr); else printf("Can‘t Find it!\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhuozhuo/p/5447639.html