源码:
/**
*哈希表
*@author 菜鸟
*@version 2014.7.15
*/
//哈希表的设计
//这里设计哈希表有两个元素项,一个是数据项,一个是状态项
#include <iostream>
#include <windows.h>
using namespace std;
typedef int DataType;
//定义节点结构,这里把次结构弄成动态的结构以便于操作未知的M(内存单元大小)
//先定义枚举类的哈希表的状态项
typedef enum{
Empty,Active,Deleted
}KindOfItem;
//定义哈希表的元素项
//哈希表的结构体
typedef struct{
DataType data;
KindOfItem info;//表示状态
}HashItem;
//哈希表结构体
typedef struct{
HashItem *hi;
int size;//数组的大小
int currentSize;//当前数组的个数
}HashTable;
//哈希表的初始化
/**
*哈希表的初始化
*@param Hashtable *h 表示传入的表地址
*@param int m 表示内存单元的大小
*@return int
*/
int HashInitiate(HashTable *h,int m){
h->size = m;
h->hi = (HashItem*)malloc(sizeof(HashItem)*m);//给哈希表申请空间
if(h->hi != NULL){
cout<<"申请空间成功!"<<endl;
return 1;
}else{
cout<<"申请空间失败!"<<endl;
return 0;
}
}
//这里要对哈希表进行插入查询删除的操作
//我们把插入操作建立在查询的基础上,先查询看是否存在若存在则不插入u,否则返回下标进行插入
/**
*查询操作
*@param HashTable *h 表示哈希表的地址
*@param DataType x 表示要查询的元素
*@return int 返回查询到的下标
*/
int HashSearch(HashTable *h, DataType x){
int i = 0,j = 0;//用来记录采用线性方法获取地址的变量
i = (int)x % h->size;
j = i;
while(h->hi[j].data != x &&h->hi[j].info == Active){//这里证明为冲突了
j = (j+1) % h->size;
if(j == i){
cout<<"没有找到!"<<endl;
return -h->size;
}
}
if(h->hi[j].info == Active &&h->hi[j].data == x){//相等找到了
cout<<"找到了!"<<endl;
return j;
}else{
cout<<"未找到!"<<endl;
return -j;
}
}
/**
*插入操作
*@param HashTable *h 表示哈希表的地址
*@param DataType x 表示要插入的元素
*@return 无
*/
void HashInsert(HashTable *h,DataType x){
int i = 0;//用来记录查找的结果
i = HashSearch(h,x);
if(i >0 ){//表明查找到了
cout<<"这时找到了!"<<endl;
return ;
}else if(i != -h->size){
//进行插入操作
h->hi[-i].data = x;
h->hi[-i].info = Active;
h->currentSize++;
cout<<"插入成功!"<<endl;
return ;
}else{
cout<<"冲突且未找到!"<<endl;
return ;
}
}
/**
*删除操作
*@param HashTable *h 表示哈希表的地址
*@param DataType x 表示要删除的元素
*return 无
*/
void HashDelete(HashTable *h,DataType x){
int i = 0;//记录要删除的位置
i = HashSearch(h,x);
if(i >= 0){
h->hi[i].info = Deleted;
h->currentSize--;
cout<<"删除成功!"<<endl;
}else{
cout<<"未找到,删除失败!"<<endl;
return;
}
}
//撤销哈希表
void HashDestroy(HashTable *h){
free(h->hi);
cout<<"撤销成功!"<<endl;
}
//输出
void HashOutPut(HashTable *h){
for(int i = 0;i < h->size;i++){
if(h->hi[i].info == Active){
cout<<"元素:"<<h->hi[i].data<<endl;
}else{
continue;
}
}
}
//菜单
void menu(){
cout<<" |----------------------------------------------|"<<endl;
cout<<" |-------------- 菜单 ---------------|"<<endl;
cout<<" |--------------1、查询 ---------------|"<<endl;
cout<<" |--------------2、插入 ---------------|"<<endl;
cout<<" |--------------3、删除 ---------------|"<<endl;
cout<<" |--------------4、撤销 ---------------|"<<endl;
cout<<" |--------------5、输出 ---------------|"<<endl;
cout<<" |----------------------------------------------|"<<endl;
}
//操作
void Operation(HashTable *h){
menu();
int input = 0;
int num = 0;
cout<<"请选择需要的服务:1-5"<<endl;
cin>>input;
if(input <1||input>5){
Operation(h);
}
switch(input){
case 1:cout<<"进入的是查询操作------------"<<endl;if(h == NULL){cout<<"为空!"<<endl;}else{cout<<"请输入要查询的元素:";cin >> num;HashSearch(h,num);}Operation(h);break;
case 2:cout<<"进入的是插入操作------------"<<endl;if(h == NULL){cout<<"为空!"<<endl;}else{cout<<"请输入要插入的元素:";cin >> num;HashInsert(h,num);}Operation(h);break;
case 3:cout<<"进入的是删除操作------------"<<endl;if(h == NULL){cout<<"为空!"<<endl;}else{cout<<"请输入要删除的元素:";cin >> num;HashDelete(h,num);}Operation(h);break;
case 4:cout<<"进入的是撤销操作------------"<<endl;if(h == NULL){cout<<"为空!"<<endl;}else{HashDestroy(h);}Operation(h);break;
case 5:cout<<"进入的是输出操作------------"<<endl;if(h == NULL){cout<<"为空!"<<endl;}else{HashOutPut(h);}Operation(h);break;
}
}
int main(){
int m = 0;//要输入的内存单元的个数
cout<<"请输入要设定的内存单元的个数:";
cin>>m;
HashTable h;
HashInitiate(&h,m);
Operation(&h);
system("PAUSE");
return 0;
}