[資料結構]Hash表初學(開放定址法 )
阿新 • • 發佈:2019-01-25
/* Name:Hash表初學 (陣列實現連結串列 開放定址法 ) Actor:HT Time:2015年9月29日 Error Reporte: */ #include"stdio.h" #include"string.h" #include"stdlib.h" int hash[9000]; int value[9000]; int f1(int x) { return x % 8997; } int f2(int x) { return x % 8996; } int fhash(int x,int t) //hash函式 { return f1(x)+t*f2(x); } void add(int x) //新增 { int i,j; for (i = 0; hash[fhash(x, i)] != 0; i++) {} hash[fhash(x, i)] = 1; //標記而已 value[fhash(x, i)] = x; } void serach(int x)//查詢 { int i; for (i = 0; hash[fhash(x, i)] != 0; i++) { if (value[fhash(x, i)] == x) { printf("find it!\n"); return; } } printf("查無此值\n"); } void vis() { int i; int temp; for (i = 0; i < 9000; i++) { if (hash[i] == 0) continue; printf("%d\n", value[i]); } } int main() { memset(hash, 0, sizeof(hash)); memset(value, 0, sizeof(value)); for (int i = 1; i < 5; i++) { add(i); } vis(); system("pause"); return 0; }