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 ...
随机推荐
- 组件-实体-系统 Entiy-Compoent-System ECS架构整理
继承体系的问题,为什么要用ECS 面向对象的问题 当一个新的类型需要多个老类型的不同功能的时候,不能很好的继承出来 游戏开发后期会有非常多的类,很难维护 游戏中子系统很多,它们对一个对象的关注点往往互 ...
- 在ios Xcode10下小白都能解决library not found for -libstdc++.6.0.9
写在前面 library not found for -libstdc++.6.0.9,今天做项目的时候碰到这个问题,解决的过程中遇到了目录路径不对的问题(不在通常的/Applications/Xco ...
- vs2013 安装 mvc5 的方法
工具-->NuGet程序包管理器-->程序包管理器控制台 然后 PM>Install-Package Microsoft.AspNet.Mvc -Version 5.0.0
- 题解报告:hdu 1075 What Are You Talking About
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...
- 表达式语言EL简单学习
Jsp2.0最重要的特性就是表达式语言EL.jsp用户可以用它来访问应用程序数据. EL表达式以${开头并以}结束. ${expresion} ${x+y} 它也常用来连接两个表达式,取值将从 ...
- 452 Minimum Number of Arrows to Burst Balloons 用最少数量的箭引爆气球
在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够了.开始坐标总是小于结束坐标.平面 ...
- DEV—【GridControl主从表】
先附上效果图,不是想要这个效果的朋友就不用可以继续寻找了. DEV—GridControl制作主从表: (注:此例没有用到数据库,只是单纯的在内存中操作数据.) 写这一笔,是为了能更好的理解主从表,的 ...
- JDK11源码分析之集合类(一)----HashMap
一,首先需要拉取JDK11源码: 方便起见我给出芋道源码作者已经拉取好的openJDK11的GitHub地址只需要fork一下克隆到本地导入IDEA中就可以对源码分析了: https://github ...
- react学习文档
转自http://www.ruanyifeng.com/blog/2015/03/react.html,阮一峰老师的博客. 最近想学习react,官方文档的例子不是那么浅显易懂,看了相关博客,觉得阮一 ...
- vue2.0 动态切换组件
组件标签是Vue框架自定义的标签,它的用途就是可以动态绑定我们的组件,根据数据的不同更换不同的组件. <!DOCTYPE html> <html lang="en" ...