由哈希表的定义,采用C++完成了一个学生成绩存储系统,分析过程如下:

由于哈希表是按KEY值存储,我们假设KEY值为一个字符串。hash算法为字符串的前两位大写字母所对应的数字对一个质数的模运算。

int Hash(KeyType c)
{
int n = c[] - 'A'+c[]-'A'+;
return n % Mod;
}

哈希表的类定义如下

class HashTable
{
public:
// HashTable();
HashTable(int len);
~HashTable();
bool SearchHash(KeyType K, int &p, int &c,int &s);
bool InsertHash(ElemType e);
private:
ElemType * elem;
int count;
int sizeindex;
};

搜索函数SearchHash

bool HashTable::SearchHash(KeyType K, int &p, int &c,int &s)
{
c = ;
p = Hash(K);
std::string NULLKey = "None";
while (this->elem[p].key != NULLKey && K != this->elem[p].key)
{
p++;
c++;//冲突次数++
}
// cout << "after,p = " << p << endl;
if (K == this->elem[p].key)
{
s = elem[p].score;
return true;
}
else
return false;
}

插入函数InsertHash

bool HashTable::InsertHash(ElemType e)
{
int c = ;//c为冲突次数
int p,s;
if (SearchHash(e.key, p, c,s))
return false;
else if (c < Hashsize[this->sizeindex] / )
{
this->elem[p] = e;
this->count++;
return true;
}
else
{
HashTable();
return false;
}
}

任务源代码如下:

哈希表元素类

 //ElemType.h

 #ifndef ELEMTYPE_H
#define ELEMTYPE_H
#include<string>
typedef std::string KeyType;
class ElemType
{
public:
KeyType key;
int score;
};
#endif

哈希表类

 //HashTable.h

 #ifndef HASHTABLE_H
#define HASHTABLE_H
#include"ElemType.h" class HashTable
{
public:
// HashTable();
HashTable(int len);
~HashTable();
bool SearchHash(KeyType K, int &p, int &c,int &s);
bool InsertHash(ElemType e);
private:
ElemType * elem;
int count;
int sizeindex;
}; #endif

哈希表的操作函数

 //HashTable.cpp

 #include"HashTable.h"
#include<iostream>
using namespace std;
const int Mod = ;//质数模
int Hashsize[] = { ,,, };
int Hash(KeyType c)
{
int n = c[] - 'A'+c[]-'A'+;
return n % Mod;
} HashTable::HashTable(int len=)
{
elem = new ElemType[len];
for (int i = ; i < len; i++)
{
elem[i].key = "None";
}
count = ;
sizeindex = ;
} HashTable::~HashTable()
{
delete[] elem;
count = ;
sizeindex = ;
} bool HashTable::SearchHash(KeyType K, int &p, int &c,int &s)
{
c = ;
p = Hash(K);
std::string NULLKey = "None";
while (this->elem[p].key != NULLKey && K != this->elem[p].key)
{
p++;
c++;//冲突次数++
}
// cout << "after,p = " << p << endl;
if (K == this->elem[p].key)
{
s = elem[p].score;
return true;
}
else
return false;
} bool HashTable::InsertHash(ElemType e)
{
int c = ;//c为冲突次数
int p,s;
if (SearchHash(e.key, p, c,s))
return false;
else if (c < Hashsize[this->sizeindex] / )
{
this->elem[p] = e;
this->count++;
return true;
}
else
{
HashTable();
return false;
}
}

信息存储的实现

 //Main.cpp

 #include"HashTable.h"
#include<iostream>
using namespace std;
int main()
{
HashTable* H = new HashTable();
ElemType e;
int p,c;
int i=;
KeyType k;
int score;
while (i != )
{
switch (i)
{
case :
cout << "***______HashTable_____***" << endl;
cout << "***___1.Display menu___***" << endl;
cout << "***___2.Insert_________***" << endl;
cout << "***___3.Search_________***" << endl;
cout << "***___0.Exit___________***" << endl;
break;
case :
cout << "input name with A~Z: ";
cin >> k;
e.key = k;
cout << "input score with number: ";
cin >> score;
e.score = score;
H->InsertHash(e);
break;
case :
cout << "input name with A~Z: ";
cin >> k;
if (H->SearchHash(k, p, c, score))
{
cout << k << "'s score is " << score << endl;;
}
else
cout << "can't found" << endl;
break; }
cout << "输入选项:";
cin >> i;
}
system("pause");
return ;
}

效果如下

***______HashTable_____***
***___1.Display menu___***
***___2.Insert_________***
***___3.Search_________***
***___0.Exit___________***
输入选项:
input name with A~Z: DSG
input score with number:
输入选项:
input name with A~Z: QWER
input score with number:
输入选项:
input name with A~Z: DSG
DSG's score is 99
输入选项:
input name with A~Z: OOP
input score with number:
输入选项:
input name with A~Z: QWER
QWER's score is 70
输入选项:
input name with A~Z: OOP
OOP's score is 100
输入选项:
请按任意键继续. . .

HashTable的C++实现的更多相关文章

  1. HashSet HashTable 与 TreeSet

    HashSet<T>类 HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet& ...

  2. Javascript实现HashTable类

    散列算法可以尽快在数据结构中找出指定的一个值,因为可以通过Hash算法求出值的所在位置,存储和插入的时候都按照Hash算法放到指定位置. <script> function HashTab ...

  3. Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结

    2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...

  4. java面试题——HashMap和Hashtable 的区别

    一.HashMap 和Hashtable 的区别 我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Clonea ...

  5. Map集合及与Collection的区别、HashMap和HashTable的区别、Collections、

    特点:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map集合和Collection集合的区别 Map集合:成对出现 (情侣)                       ...

  6. HashTable初次体验

    用惯了数组.ArryList,初次接触到HashTable.Dictionary这种字典储存对于我来说简直就是高大上. 1.到底什么是HashTable HashTable就是哈希表,和数组一样,是一 ...

  7. HashMap和 Hashtable的比较

    Hashtable 和 HashMap的比较 1.  HashMap可以接受null(HashMap可以接受为null的键值(key)和值(value), HashTable不可以接受为null的键( ...

  8. hashMap和hashTable的区别

    每日总结,每天进步一点点 hashMap和hashTable的区别 1.父类:hashMap=>AbstractMap hashTable=>Dictionary 2.性能:hashMap ...

  9. SortedList和HashTable

    都是集合类,C#中同属命名空间System.Collections,“用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写:value用于存储对应于key的 ...

  10. Java Hashtable的实现

    先附源码: package java.util; import java.io.*; /** * This class implements a hash table, which maps keys ...

随机推荐

  1. bzoj 3751: [NOIP2014]解方程【数学】

    --我真是太非了,自己搞了7个质数都WA,从别人那粘5个质数就A了-- 就是直接枚举解,用裴蜀定理计算是否符合要求,因为这里显然结果很大,所以我们对多个质数取模看最后是不是都为0 #include&l ...

  2. bzoj 2811: [Apio2012]Guard【线段树+贪心】

    关于没有忍者的区间用线段树判就好啦 然后把剩下的区间改一改:l/r数组表示最左/最右没被删的点,然后删掉修改后的左边大于右边的:l升r降排个序,把包含完整区间的区间删掉: 然后设f/g数组表示i前/后 ...

  3. 开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等

    开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等 C++开发网络通信程序时用asio是个不错的选择 ...

  4. header的参数不能带下划线

    移动端把一些公共参数放在了 header 了, 在 laravel 中使用 use \Illuminate\Http\Request; //这个是获取所有header信息Request::header ...

  5. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  6. 洛谷 P1816 忠诚

    https://www.luogu.org/problemnew/show/1816 st表模板 #include<cstdio> #include<algorithm> us ...

  7. ACM_数数?诶?这么简单?

    数数?诶?这么简单? Time Limit: 2000/1000ms (Java/Others) Problem Description: 当看到GDUFE-GAME宣传海报上提到"场内人员 ...

  8. 选择排序 分类: 算法 c/c++ 2014-10-10 20:32 509人阅读 评论(0) 收藏

    选择排序(假设递增排序) 每次选取从当前结点到末尾结点中最小的一个与当前结点交换,每一轮固定一个元素位置. 时间复杂度O(n^2),空间复杂度O(1).下面的示例代码以带头结点的链表为存储结构: #i ...

  9. SpringBoot_整合视图层技术

    SpringBoot整合视图层技术 在目前的企业级应用开发中,前后端分离是趋势,但是视图层技术还占有一席之地.Spring Boot对视图层技术提供了很好的支持,官方推荐使用的模板引擎是Thymele ...

  10. Android 在代码中安装 APK 文件

    废话不说,上代码 private void install(String filePath) { Log.i(TAG, "开始执行安装: " + filePath); File a ...