-
1.题目描述
哈希函数为H(key)=key%13,哈希冲突处理方法为:链地址法
![](https://i-blog.csdnimg.cn/blog_migrate/8880a113067662d54532d3405f1231f3.jpeg)
-
2.代码实现
首先建立一个哈希链表结点类HashNode,用于存放处理冲突时的关键字,其数据成员为一个数据域存放关键字,一个指针域指向下一关键字结点
该类相当于此图片中的链表中的结点
class HashNode//哈希链表结点
{
public:
int key;//关键字域
HashNode *node;//下一个关键字结点
};
HashList类声明为哈希链表类,其中每个哈希地址对应的头节点,指向哈希结点
此图中的13个哈希地址,每一个均包含一个头节点,指向该链表下的第一个结点
class HashList//哈希链表
{
public:
HashNode *head;//头节点指针域
};
最后定义一个哈希表类HashTable,此为主要处理类
class HashTable//哈希表
{
public:
//构造函数
HashTable(int size)
{
m_length = size;
Array = new HashList[size];
}
int HashFunction(int key);//哈希函数
void HashInit();//初始化哈希表
void CreateHashTable();//创建哈希表
void PrintHashTable();//打印哈希表
private:
HashList *Array;//哈希数组
int m_length;//哈希表长
};
整个代码实现如下,创建哈希函数中我写了两种插入方法,一种为直接尾部插入,没有排序,另一种在插入之前选择插入位置按升序排序进行插入,后者相对比较抽象,线性表没有做过的可能难以理解,看懂前者我相信后者多花点时间可以理解的
#include<iostream>
using namespace std;
/*
以除留余数法构建哈希函数
哈希函数为H(key)=key%13
*/
class HashNode//哈希链表结点
{
public:
int key;//关键字域
HashNode *node;//下一个关键字结点
};
class HashList//哈希链表
{
public:
HashNode *head;//头节点指针域
};
class HashTable//哈希表
{
public:
//构造函数
HashTable(int size)
{
m_length = size;
Array = new HashList[size];
}
int HashFunction(int key);//哈希函数
void HashInit();//初始化哈希表
void CreateHashTable();//创建哈希表
void PrintHashTable();//打印哈希表
private:
HashList *Array;//哈希数组
int m_length;//哈希表长
};
//哈希函数
int HashTable:: HashFunction(int key)
{
return key % 13;
}
//哈希表初始化
void HashTable::HashInit()
{
if (0 == m_length)
{
return;
}
else
{
for (int i = 0; i < m_length; i++)
{
Array[i].head = nullptr;//每个哈希地址中的head域为空,表示暂无存放关键字
}
}
}
//创建哈希表
void HashTable::CreateHashTable()
{
int key;
int address;//经过哈希函数变换得到的哈希地址
HashNode *head = nullptr;
for (int i = 0; i < m_length-1; i++)
{
cin >> key;
address = HashFunction(key);
head = Array[address].head;//定义临时HashNode指针,保存哈希地址中的头结点
if (!head)//经由哈希函数得到的哈希地址暂未存放任何关键字
{
Array[address].head = new HashNode;//分配一个新的哈希链表结点
Array[address].head->key = key;//当前结点数据域赋值为此关键字
Array[address].head->node = nullptr;//当前结点指针域赋值为空
}
else//经由哈希函数得到的哈希地址已存放关键字,则当前关键字存放到当前地址中的哈希链表中
{
///*
// 此为处理冲突时,关键字直接插入到哈希链表尾部,无排序
//*/
当前位置非空的时候
//while (head->node)
//{
// //临时指针指向哈希链表下一个位置
// head = head->node;
//}
下一个位置为空时结束循环,当前关键字插入到下个位置
//head->node = new HashNode;
//head->node->key = key;
//head->node->node = nullptr;
/*
此为处理冲突时,关键在在哈希链表中按升序排序
*/
//当前哈希地址对应的哈希链表中的第一个结点关键字大于当前关键字,则在该结点之前插入当前关键字
if (head->key > key)
{
HashNode *pnew = new HashNode;//开辟一段空间
pnew->key = key;//赋值
pnew->node = head;//当前结点指向第一个结点
Array[address].head = pnew;//头指针指向当前结点
}
else
{
//比较下一个关键字
while (head->node&&head->node->key < key)
{
head = head->node;
}
//退出循环,如果head的下一个结点不为空,说明head->node->key>key,key插在head之前
if (head->node)
{
HashNode *pnew = new HashNode;
pnew->key = key;
pnew->node = head->node;
head->node = pnew;
}
else//head的下一个结点为空,key直接插在head之后
{
head->node = new HashNode;
head->node->key = key;
head->node->node = nullptr;
}
}
}
}
}
//打印哈希表
void HashTable::PrintHashTable()
{
//循环遍历哈希表
for (int i = 0; i < m_length; i++)
{
//当前哈希地址为空,输出NULL
if (!Array[i].head)
{
cout << "Array[" << i << "]" << "->" << "NULL" << endl;
}
else
{
HashNode *p = Array[i].head;//定义临时指针保存哈希地址中的头节点
cout << "Array[" << i << "]" << "->";
while (p)
{
cout<< p->key << "->";
p = p->node;//指向下一个关键字结点
}
cout << "NULL" << endl;
}
}
}
int main()
{
HashTable Hash(13);//创建哈希表对象Hash,哈希表长为13
Hash.HashInit();//哈希表初始化
Hash.CreateHashTable();//创建哈希表
Hash.PrintHashTable();//打印哈希表
return 0;
}
运行截图,此为排序后的