实验内容及要求:
编写控制台应用程序,提供以下菜单项:
- 插入关键字
- 删除关键字
- 查找关键字
- 结束程序
其中,“插入关键字”是指从键盘输入一个关键字,将关键字插入哈希表中,若插入的关键字已存储于哈希表中,则插入失败,显示提示信息;若插入关键字数目已超过哈希表设计容量,则插入失败,显示提示信息;其它情况则插入成功,显示提示信息。程序初始运行时,哈希表为空,通过插入多个关键字建立哈希表。
“删除关键字”是指从键盘输入一个关键字,若在哈希表中查找成功,则将关键字从哈希表中删除;若查找失败,显示提示信息。
“查找关键字”是指从键盘输入一个关键字,在哈希表中查找,显示查找成功与失败的提示信息。
已知哈希函数H(K)=K mod m,其中m为哈希表长度(程序中m应不小于10)。可选择用二次探测再散列或链地址法解决冲突。若选用二次探测再散列,装填因子设为0.8;若选用链地址法,要求哈希表允许的关键字最大数目为2m。
提示:选用二次探测再散列时,空闲元素位置应存入“哑元素”占位,以标识元素位置空闲。
实验目的:掌握哈希表的建立与查找。
PS:本代码采用链地址法。并且只经过了简要测试,可能存在bug
友情链接:同学自己搭建的博客网站https://bzyl.site
#include <iostream>
using namespace std;
typedef struct Node {
int data = 0;
bool flag = false;
Node* next = nullptr;
}Node;
class Menu {
private:
Node* hash_map;
int map_length;
int cur_length;
int HashFunction(int key) const;
static void DisplayMenu();
void Insert();
void Delete();
void LookUp();
public:
explicit Menu(int length);
void Start();
~Menu();
};
Menu::Menu(int length) {
while (length <= 10) {
cout << "Too short! Retype a number which is more than 10!" << endl;
cin >> length;
}
map_length = length;
hash_map = new Node[length];
cur_length = 0;
}
Menu::~Menu() {
delete hash_map;
}
int Menu::HashFunction(int key) const {
return key % (2 * map_length);
}
void Menu::DisplayMenu() {
cout << "------------------------------------------" << endl;
cout << " 1.Insert key word " << endl;
cout << " 2.Delete key word " << endl;
cout << " 3.Look up key word " << endl;
cout << " 4.Exit " << endl;
cout << "------------------------------------------" << endl;
}
void Menu::Insert() {
int key;
cout << "Please enter a key: ";
cin >> key;
int position = HashFunction(key);
auto p = &hash_map[position];
if (cur_length + 1 >= map_length) {
cout << "The current length has exceed allowed length!" << endl;
cout << "Insertion failed!" << endl;
return ;
}
if (p->flag) {
if (p->data == key) {
cout << "Insertion failed! Key exists!" << endl;
return ;
} else {
while(p) {
if (p->data == key) {
cout << "Insertion failed! Key exists!" << endl;
return ;
} else {
if (p->next) {
p = p->next;
} else {
auto new_node = new Node;
new_node->data = key;
new_node->flag = true;
p->next = new_node;
cur_length++;
break;
}
}
}
}
} else {
hash_map[position].data = key;
hash_map[position].flag = true;
cur_length++;
}
cout << "Insertion " << key << " succeed!" << endl;
}
void Menu::Delete() {
int key;
cout << "Please enter the key: ";
cin >> key;
int position = HashFunction(key);
auto p1 = &hash_map[position];
auto p2 = &hash_map[position];
bool flag = false;
while (p1->flag && p1) {
if (p1->data == key) {
if (p1 == p2) {
if (p1->next) {
hash_map[position] = *p1->next;
flag = true;
break;
} else {
p1->flag = false;
p1->data = 0;
flag = true;
break;
}
} else {
if (p1->next) {
p2->next = p1->next;
p1->next = nullptr;
flag = true;
delete p1;
break;
} else {
p2->next = nullptr;
flag = true;
delete p1;
break;
}
}
} else {
p1 = p1->next;
p2 = p1;
}
}
if (!flag) {
cout << "Delete failed!" << key << " doesn't exist!" << endl;
} else {
cout << "Delete succeed!" << endl;
}
}
void Menu::LookUp() {
int key;
cout << "Please enter the key: " << endl;
cin >> key;
int position = HashFunction(key);
auto p1 = &hash_map[position];
int flag = false;
while (p1->flag && p1) {
if (p1->data == key) {
flag = true;
break;
} else {
p1 = p1->next;
}
}
if (flag) {
cout << "Look up succeed!" << endl;
cout << key << " is in the hash map!" << endl;
} else {
cout << "Look up failed!" << endl;
cout << key << " doesn't exist!" << endl;
}
}
void Menu::Start() {
int choice;
while (true) {
DisplayMenu();
cin >> choice;
switch (choice) {
case 1:
Insert();
break;
case 2:
Delete();
break;
case 3:
LookUp();
break;
case 4:
exit(0);
default:
break;
}
}
}
int main() {
cout << "Please enter the max number the hash map can restore: " << endl;
int number;
cin >> number;
Menu menu(number);
menu.Start();
return 0;
}
文章评论