本文摘自:哈希表的实现
代码:
// HashTable.cpp : 定义控制台应用程序的入口点
//哈希表的实现
//哈希函数采用除留余数法构造
//使用链地址法解决冲突
#include "stdafx.h"
#include <iostream>
using namespace std;
const int MAXSIZE = 100;
int a[MAXSIZE];
//哈希表的结点类型
class HashNode
{
public:
HashNode():next(NULL){};//默认构造函数
HashNode(int item):data(item), next(NULL){};//一般构造函数
private:
int data;
HashNode *next;
friend class HashTable;
};
//哈希表类型
class HashTable
{
public:
HashTable();
void Create(int *a, int n);//创建哈希表
bool Find(int data);//查找
void Insert(int data);//插入
void Delete(int data);//删除
private:
HashNode *value[10];//哈希表长度为10
};
HashTable::HashTable()//默认构造函数
{
memset(value, NULL, 10*sizeof(HashNode*));
}
void HashTable::Create(int *a, int n)
{
cout << "请输入n个元素:" << endl;
for (int i=0; i<n; i++)
{
cin >> a[i];
Insert(a[i]);
}
}
bool HashTable::Find(int data)
{
HashNode *pNode = NULL;
if (value[data%10] == NULL)//该结点不存在,则返回false
{
return false;
}
pNode = value[data%10];//获取所在位置对应的链表的第一个结点
while(pNode ->next != NULL && pNode->data != data)
{
pNode = pNode->next;
}
if (pNode != NULL)
{
return true;
}
else
{
return false;
}
}
void HashTable::Insert(int data)
{
HashNode *pNode = NULL;
if (value[data%10] == NULL)//获取所在位置对应的链表为空,则插入
{
pNode = new HashNode;
pNode->data = data;
value[data%10] = pNode;
}
else//所在位置对应的链表不空
{
if (Find(data))//检查该数值是否已经存在,若存在则返回
{
return;
}
else//否则插入链表尾端
{
pNode = value[data%10];
while(pNode->next != NULL)
{
pNode = pNode->next;
}
HashNode *newNode = new HashNode(data);
pNode->next = newNode;
}
}
}
void HashTable::Delete(int data)
{
HashNode *pNode = NULL;//定位到待删除的结点
HashNode *pPre = NULL;//定位到待删除结点的前一个结点
if (!Find(data))
{
cout << "该数值的结点不存在!" << endl;
}
else
{
pNode = value[data%10];//初始值为所在位置对应的链表的第一个结点
if(pNode->data == data)//头结点即为待删除的结点
{
value[data%10] = pNode->next;
cout << data << "删除成功!" << endl;
}
else//否则指针后移,找到待删除的结点
{
pPre = pNode;
while(pNode->next != NULL && pNode->next->data != data)
{
pPre = pNode;
pNode = pNode->next;
}
pPre->next = pNode->next;
delete pNode;
pNode = NULL;
cout << data << "删除成功!" << endl;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 0;
cout << "请输入哈希表元素的个数:";
cin >> n;
HashTable *ht = new HashTable;
ht->Create(a, n);
cout << ht->Find(9) << endl;
cout << ht->Find(3) << endl;
ht->Delete(10);
ht->Delete(8);
cout << ht->Find(8) << endl;
system("pause");
return 0;
}