[数据结构]Hash表初学(数组链表)
2015-09-27 11:31
543 查看
/* Name:Hash表初学 (数组实现链表 h(x) = x mod n ) Actor:HT Time:2015年9月27日 Error Reporte: 1.add函数 来龙去脉理解清楚 */ #include"stdio.h" #include"string.h" #include"stdlib.h" int hash[9]; //9位表 int link[10000]; //单个数组构成的N个链表,下标、数值都是序号 简直黑科技 int value[10000]; //下标序号,数值就是真值 int rear; //最后的一个序号,用来安排空的地方 int fhash(int x) //hash函数 { return x % 9; } void add(int x) //添加 { int temp = hash[fhash(x)]; while (temp != 0) { if (value[link[temp]] == x) return; temp = link[temp]; } value[rear] = x; link[rear] = hash[fhash(x)]; //连接到原队首 hash[fhash(x)] = rear; //自己成为队首 rear++; } void serach(int x)//查找 { int temp = hash[fhash(x)]; while (temp != 0) { if (value[link[temp]] == x) { printf("此值存在\n"); return; } temp = link[temp]; } printf("查无此值\n"); } void vis() { int i; int temp; for (i = 0; i < 9; i++) { printf("第%d个hash槽: ",i); temp = hash[i]; if (hash[i] == 0) { printf("Empty\n"); continue; } while (temp != 0) { printf("%d ", value[temp]); temp = link[temp]; } printf("\n"); } } void del(int x)//删除 { ;//暂不用数组实现,没有意义... ...用链表就easy多了 } int main() { memset(hash, 0, sizeof(hash)); memset(link, 0, sizeof(link)); rear = 1; for (int i = 1; i < 5; i++) { add(i); } vis(); system("pause"); return 0; }
相关文章推荐
- 数据结构顺序栈的创建、初始化、出栈、入栈及销毁
- 数据结构基础 之 最长递增子序列
- (数据结构笔记)二叉查找树的实现
- 数据结构学习笔记——树
- 数据结构学习总结——预备知识
- 数据结构学习总结——数据结构概述
- 2,Add Two Number (LinkList Math)
- 1,Two sum (Hashtable Array)
- Java数据结构与算法之有序数组的插入和合并
- iOS —— JSON 数据结构及其解析方式
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 03-树3 Tree Traversals Again (25分)
- iOS ——XML数据结构及其两种解析数据的方式SAX 、DOM
- 【软考之路】-数据结构
- 数据结构中的概念问题
- 数据结构与算法 Big O 备忘录与现实
- 软考---数据结构
- 数据结构和算法
- 数据结构学习---中缀表达式转后缀表达式
- 数据结构学习---中缀表达式转后缀表达式
- 【数据结构与算法】Hash表