由哈希表的定义,采用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. 【性能测试】服务器资源监测工具sar安装

    [root@yyy ~]# sar Cannot open /var/log/sa/sa19: No such file or directory 在Linux系统中,运行sar命令,发现无法执行: ...

  2. bzoj 3470: Freda’s Walk【拓扑排序+期望dp】

    dfs会T,只好正反两遍拓扑了-- #include<iostream> #include<cstdio> #include<queue> #include< ...

  3. bzoj 5277: [Usaco2018 Open]Out of Sorts【冒泡排序瞎搞】

    首先考虑快排的递归什么时候停下,显然是当前段只剩下一个数了,也就是一个数两边出现分隔符 然后再考虑计算冒泡长度这个操作,因为有分割,所以我们可以把这些放到一起冒泡,这和递归每个区间冒泡是等价的 所以答 ...

  4. 【微信公众号开发】根据openId群发消息

    根据开发文档可知,只要使用POST方式提交固定格式的json字符串到那个地址即可.这里我写的是最简单的文本 第一步:建立对应的实体类. package cn.sp.bean; import java. ...

  5. python之对堆栈、队列处理操作(转载+个人看法)

    参考链接:https://blog.csdn.net/u010786109/article/details/40649827 python实现堆栈操作 堆栈是一个后进先出的数据结构,其工作方式就像一堆 ...

  6. ROS学习笔记六:xxx.launch文件详解

    每当我们需要运行一个ROS节点或工具时,都需要打开一个新的终端运行一个命令.当系统中的节点数量不断增加时,每个节点一个终端的模式会变得非常麻烦.那么有没有一种方式可以一次性启动所有节点呢?答案当然是肯 ...

  7. [USACO 2012 Mar Gold] Large Banner

    传送门:http://www.usaco.org/index.php?page=viewproblem2&cpid=127 又是一道这种题目,遇到一次跪一次,这次终于硬着头皮看懂了题解,但是谢 ...

  8. InputStream和OutputStream的一遍博客 分析非常到位

    http://www.cnblogs.com/springcsc/archive/2009/12/03/1616187.html

  9. $ ssh -T git@github.com ssh: connect to host ssh.github.com port 22: Connection timed out

    在C:/用户/用户名/.ssh中添加几个文件 之前的电脑生成都是四个文件,分别是 id_rsa  id_rsa.pub  config known_hosts 不知道为什么在另一台电脑上却生成两个文件 ...

  10. Node.js——npm

    npm un 包名 :删除指定包,不删除安装的依赖 npm un --save 包名: 删除包,并且删除其依赖项 npm install -g cnpm --registry=https://regi ...