哈希表(Hash Table)
哈希表是一种高效的数据结构,用于实现键-值对的存储和查找。它通过哈希函数将键映射到数组的特定位置,从而允许常数时间内的插入、查找和删除操作。哈希表在很多编程场景中都得到广泛应用,例如数据库索引、缓存、字典等。
哈希函数
哈希表的核心是哈希函数,它将键转化为数组索引。好的哈希函数应该将不同的键分散到不同的索引位置,最小化冲突。
冲突处理
冲突是指多个键被映射到了同一个索引位置。常见的冲突处理方法包括链地址法(在每个索引位置维护一个链表)、开放地址法(尝试找到下一个可用的索引位置)等。
插入、查找和删除
通过哈希函数确定键的索引位置,然后在该位置执行对应的操作。由于哈希函数的高效性,这些操作通常是常数时间复杂度。
这一大堆巴拉巴拉都是我专门找的专业文档copy的,看看得了,就当是个引子。真要认真钻研上边的文字不如去找个视频看看,有图有动画的,越看越乐呵。人间清醒,还是得看代码。
来人!喂公子吃饼给公子上代码!
#include <iostream>
#include <string>
#include <vector>
#include <list>
using namespace std;
#define TABLE_SIZE 10;
class HuahTable
{
public:
int size;
vector<vector<string>> table ; //demo使用v套v;
vector<list<string>> table1;//实际如果数据量大且需要频繁改动则换v套l;
//计算哈希值,ASCALL码计算
int HushFunction(string key) {
int hash = 0;
for (char c : key) {
hash += c;
}
cout << key << " : " << hash << endl;
return hash % TABLE_SIZE;
}
//插入数据
void insert() {
string getstr;
cout << "请输入你想插入的数据" << endl;
getline(cin, getstr);
int index = HushFunction(getstr);
bool isin = false; //加个判断,判断数据是否存在。
for (auto it : table[index]) {
if (it == getstr) {
cout << getstr << "已经存在了" << endl;
isin = true;
}
}
if (!isin) {
table[index].push_back(getstr);
}
}
//查找key
int findkey() {
string getstr;
cout << "请输入你想查询的数据" << endl;
getline(cin, getstr);
int index = HushFunction(getstr);
bool found = false;
for (auto it : table[index]) {
if (it == getstr) {
cout << getstr << "存在" << endl;
found = true;
}
}
if (!found) {
cout << "不存在" << getstr << endl;
}
return -1;
}
//删除key
int delete1() {
string getstr;
cout << "请输入你想删除的数据" << endl;
getline(cin, getstr);
int index = HushFunction(getstr);
for (auto i = 0; i < table[index].size();i++) {
if (table[index][i] == getstr) {
table[index][i] = "";
if (table[index][i].empty()) {
cout << "删除" << getstr << "成功" << endl;
}
else {
cout << "删除" << getstr << "失败" << endl;
return -1;
}
}
}
}
//修改key,感觉这个功能很鸡肋,但是讲究个 “增删改查” 没办法,必须得加上。
int Edit1() {
string getstr;
string getchenge;
cout << "请输入你想修改的数据" << endl;
getline(cin, getstr);
int index = HushFunction(getstr);
bool isin = false;
for (auto i = 0; i < table[index].size(); i++) {
if (table[index][i] == getstr) {
table[index][i] = "";
isin = true;
}
}
if (!isin) {
cout << getstr << "不存在!" << endl;
return -1;
}
cout << "你想要将" << getstr << "修改为什么?" << endl;
getline(cin, getchenge);
int index1 = HushFunction(getchenge);
//cout << index << endl;
for (auto it : table[index1]) {
if (it == getchenge) {
cout << getchenge << "已经存在了" << endl;
isin = true;
return -1;
}
}
if (isin) {
table[index1].push_back(getchenge);
cout << "修改成功!" << endl;
}
}
//构造,初始化table大小 。没写析构,让他自己析构就完事了,容器自带的功能多写这一点干嘛
HuahTable(int size) : size(size) {
table.resize(size);
}
};
//每一行代码都是有他自己的作用的,随便删出了问题你全责!!!!!
int main() {
HuahTable HuahTable(10); //因为构造的原因这里没法写TABLE_SIZE,可惜了
cout << "请按照下表操作" << endl;
while(1){
cout << "1 ------ 添加数据" << endl;
cout << "2 ------ 查找数据" << endl;
cout << "3 ------ 删除数据" << endl;
cout << "4 ------ 修改数据" << endl;
cout << "5 ------ 退出" << endl;
int a;
cin >> a;
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //不让换行影响输入
switch (a)
{
case 1:
HuahTable.insert();
break;
case 2:
HuahTable.findkey();
break;
case 3:
HuahTable.delete1();
break;
case 4:
HuahTable.Edit1();
break;
case 5:
return 0;
break;
default:
cout << "请输入有效的选项!" << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //清楚缓存区 + 消除换行影响
break;
}
}
return 0;
}