[数据结构]Hash表初学(开放寻址法 )

来源:互联网 发布:微擎人人商城源码下载 编辑:程序博客网 时间:2024/06/05 05:10
/*Name:Hash表初学 (数组实现链表 开放寻址法 )Actor:HTTime: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;}

0 0
原创粉丝点击