使用再哈希算法查找元素
2016-01-07 09:08
295 查看
使用再哈希算法查找元素:
输出结果:
/* hash search, using rehash method if find k, return if not find, d=(d+step)%m, rehash find */ int SearchHash(HashTable H, KeyType k) { int d, d1, m; m = H.tableSize; d = d1 = k%m; while(H.data[d].key != -1) { if(H.data[d].key == k) //hunt return d; else d = (d+1)%m; if(d==d1) return -1; //fail } return -1; }
int main(int argc, char *argv[]) { int hash[] = {23, 35, 12, 56, 123, 39, 342, 90}; int m=11, p=11, n=8, pos; int key, index; HashTable H; CreateHash(&H, m, p, hash, n); key = 123; index = SearchHash(H, key); if(index < 0) { printf("find failed!\n"); return -1; } printf("key:%d, index:%d\n", key, index); return 0; }
输出结果:
root@ubuntu:/mnt/shared/appbox/hash# ./hash [line:59] addr:1, i=0, key=23 [line:59] addr:2, i=1, key=35 [line:70] di:3, i=2, key=12 [line:70] di:4, i=3, key=56 [line:70] di:5, i=4, key=123 [line:59] addr:6, i=5, key=39 [line:70] di:7, i=6, key=342 [line:70] di:8, i=7, key=90 hash index: 0 1 2 3 4 5 6 7 8 9 10 key value: -1 23 35 12 56 123 39 342 90 -1 -1 hash times: 0 1 1 3 4 4 1 7 7 0 0 key:123, index:5 root@ubuntu:/mnt/shared/appbox/hash#
相关文章推荐
- Spring boot构建基于rest的Web服务
- J-18 集合对象
- 阿里云专属推荐码
- 深入了解DSP与ARM
- 盘点:那些出家(修行)的互联网人士
- [王垠系列]什么是语义学
- osg三维重建的两种方法剖析:三角面片(osgUtil::DelaunayTriangulator)和四角面片(osg::HeightField) (1)
- 对进制与位的理解
- C语言学习之关键字第四讲
- ListView嵌套ListView中如何得到ListView的高度
- [王垠系列]GTF - Great Teacher Friedman
- PQ格式化虚拟机硬盘如何生效
- angularjs创建弹出框实现拖动效果
- Android之Intent传递数据的方式
- BZOJ 1997: [Hnoi2010]Planar|2-SAT|二分图染色
- oracle查询优化,存储过程select表循环插入另一个表,以及索引重建
- java160104MakeNameDemo
- StreamCQL源码阅读(4) 应用程序执行
- [王垠系列]什么是“对用户友好”
- 详解iOS应用UI开发中的九宫格坐标计算与字典转换模型