HashTable的C++实现
由哈希表的定义,采用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++实现的更多相关文章
- HashSet HashTable 与 TreeSet
HashSet<T>类 HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet& ...
- Javascript实现HashTable类
散列算法可以尽快在数据结构中找出指定的一个值,因为可以通过Hash算法求出值的所在位置,存储和插入的时候都按照Hash算法放到指定位置. <script> function HashTab ...
- Java集合专题总结(1):HashMap 和 HashTable 源码学习和面试总结
2017年的秋招彻底结束了,感觉Java上面的最常见的集合相关的问题就是hash--系列和一些常用并发集合和队列,堆等结合算法一起考察,不完全统计,本人经历:先后百度.唯品会.58同城.新浪微博.趣分 ...
- java面试题——HashMap和Hashtable 的区别
一.HashMap 和Hashtable 的区别 我们先看2个类的定义 public class Hashtable extends Dictionary implements Map, Clonea ...
- Map集合及与Collection的区别、HashMap和HashTable的区别、Collections、
特点:将键映射到值的对象,一个映射不能包含重复的键,每个键最多只能映射到一个值. Map集合和Collection集合的区别 Map集合:成对出现 (情侣) ...
- HashTable初次体验
用惯了数组.ArryList,初次接触到HashTable.Dictionary这种字典储存对于我来说简直就是高大上. 1.到底什么是HashTable HashTable就是哈希表,和数组一样,是一 ...
- HashMap和 Hashtable的比较
Hashtable 和 HashMap的比较 1. HashMap可以接受null(HashMap可以接受为null的键值(key)和值(value), HashTable不可以接受为null的键( ...
- hashMap和hashTable的区别
每日总结,每天进步一点点 hashMap和hashTable的区别 1.父类:hashMap=>AbstractMap hashTable=>Dictionary 2.性能:hashMap ...
- SortedList和HashTable
都是集合类,C#中同属命名空间System.Collections,“用于处理和表现类似keyvalue的键值对,其中key通常可用来快速查找,同时key是区分大小写:value用于存储对应于key的 ...
- Java Hashtable的实现
先附源码: package java.util; import java.io.*; /** * This class implements a hash table, which maps keys ...
随机推荐
- 利用jenv安装maven, tomcat,zookeeper等
jenv有关的网站: http://jenv.io https://github.com/gcuisinier/jenv 1. 执行jenv安装 $ curl -L -s get.jenv.io | ...
- Codeforces785D - Anton and School - 2
传送门 题意 给出一个只包含'(',')'的字符序列,询问有多少个\(RSBS\) 分析 首先需要知道一个公式 \[\sum_{i=0}^{min(x,y)}C_x^i*C_y^i=C_{x+y}^x ...
- Reshapeing operations
Reshapeing operations Suppose we have the following tensor: t = torch.tensor([ [1,1,1,1], [2,2,2,2], ...
- 数字货币期货与现货JavaScript量化策略代码详解汇总
1.动态平衡策略 按照当前的 BTC 的价值,账户余额保留¥5000 现金和 0.1个 BTC,即现金和BTC 市值的初始比例是 1:1. 如果 BTC 的价格上涨至¥6000,即 BTC 市值大于账 ...
- [SPOJ375]Qtree
Description You are given a tree (an acyclic undirected connected graph) with N nodes, and edges num ...
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- Android开发学习——Android Studio配置SVN
一.基本配置 1. 下载这个,然后双击 安装,按下图这样选 然后 傻瓜式安装 2. 进入Android studio设置:Use Command Line Client 选择浏览到第1步你本地安装 T ...
- spring实现模板文件下载
前台 <form id="batchModel0" method="post" action="/common/download-file&qu ...
- VS2012出现加载失败时的解决办法 win7同样适用
今天更新了WIN8系统补丁,然后就出现了大量的问题,特别是经常用的软件像VS2012 老是加载失败,还说是缺少什么包一类的,弄了好长段时间终于解决了,现在将经验分享下 工具/原料 VS2012+WIN ...
- Eclipse打包多渠道包
下面介绍的是在eclipse中使用gradle进行多渠道打包(添加签名+混淆) 1,首先新建一个android测试项目,结构如下图所示: 2,选中右键选择expert->生成gradle文件,如 ...