散列的构成:散列函数,散列表的存储方式,散列表的冲突解决方法。

1.散列函数

  较常用的散列函数有除留余数法,数字分析法,平方取中法,折叠法。

2.散列表的存储方式

  闭散列法(开地址法),用数组存储;开散列法(链地址法),用邻接链表存储。

3.散列表的冲突解决方法

  主要是针对闭散列中关键码位置冲突的问题,常用的方法有线性探查法,二次探查法,双散列法。

性能分析:在存储方式中,开散列法优于闭散列法;在散列函数中,除留余数法最优。

实现代码:

 
 #include<iostream>
using namespace std;
enum kind{Active,Empty,Deleted};
class ArrayHashTable{//闭散列法
public:
ArrayHashTable(const int d,int sz=){
tableSize=sz;
divitor=d;
table=new int[tableSize];
info=new kind[tableSize];
for(int i=;i<tableSize;i++){
table[i]=;
info[i]=Empty;
}
}
~ArrayHashTable(){
delete[] table;
delete[] info;
}
bool findPos(int k,int &i){//寻找k关键码所在位置i
i=k%divitor;
int j=i;
do{
if(info[i]==Active&&table[i]==k)
return true;
if(info[i]==Empty)
return false;
i=(i+)%tableSize;
}
while(j!=i);
return false;
}
bool insert(int k){//插入关键码k
int i;
if(findPos(k,i))
return false;
if(info[i]!=Active){
table[i]=k;
info[i]=Active;
return true;
}else
return false;
}
bool remove(int k){//删除关键码k
int i;
if(!findPos(k,i))
return false;
table[i]=Deleted;
return true;
}
int *getArray(){
return table;
}
private:
int divitor;
int tableSize;
int *table;
kind *info;
}; class Node{
public:
int data;
Node *next;
Node(int d,Node *n=NULL){
data=d;
next=n;
}
};
class LinkedHashTable{//开散列法
public:
LinkedHashTable(int d,int sz=){
tableSize=sz;
divitor=d;
hs=new Node*[sz];
for(int i=;i<sz;i++)
hs[i]=new Node();
}
~LinkedHashTable(){
delete []hs;
}
bool findPos(int k,Node *&p,Node *&last){
int i=k%divitor;
last=hs[i];
p=hs[i]->next;
while(p!=NULL&&p->data!=k){
p=p->next;
last=last->next;
}
if(p!=NULL&&p->data==k)
return true;
else
return false;
}
bool insert(int k){
Node *p,*last;
int i=k%divitor;
if(findPos(k,p,last))
return false;
last->next=new Node(k);
return true;
}
bool remove(int k){
Node *p,*last;
if(!findPos(k,p,last))
return false;
last->next=p->next;
return true;
}
Node **getArray(){
return hs;
}
private:
int divitor;
int tableSize;
Node **hs;
}; void test(Node *&q){
q=new Node();
}
int main(){
//闭散列法
// ArrayHashTable *hs=new ArrayHashTable(11,12);
// int a[]={37,25,14,36,49,68,57,11};
// for(int i=0;i<8;i++)
// hs->insert(a[i]);
// int *array=hs->getArray();
// for(int i=0;i<12;i++){
// cout<<array[i]<<" ";
// }
// delete hs; //开散列法
// LinkedHashTable *hs=new LinkedHashTable(11,12);
// int a[]={37,25,14,36,49,68,57,11};
// for(int i=0;i<8;i++)
// hs->insert(a[i]);
// Node **array=hs->getArray();
// for(int i=0;i<12;i++){
// Node *p=array[i]->next;
// while(p!=NULL){
// cout<<p->data<<" ";
// p=p->next;
// }
// cout<<endl;
// }
// delete hs;
return ;
}

  

最新文章

  1. Maven远程仓库的配置
  2. CString转换为string
  3. YUM源
  4. shell命令从目录中循环匹配关键词
  5. wmi详解,RPC和防火墙
  6. js 删除DropDownList的选项
  7. 微信朋友圈如何同时分享(图片+文字) Android版
  8. psp开发------汉化插件
  9. Introducing the Blog Module
  10. BZOJ1642: [Usaco2007 Nov]Milking Time 挤奶时间
  11. Http 请求头中的 Proxy-Connection
  12. 设计模式-GoF23
  13. 使用jquery时一些小技巧的总结
  14. Linux 菜鸟学习笔记--系统分区
  15. 原生ajax详解
  16. WINDOWS SERVER 2016 设置使用照片查看器查看图片
  17. 从零开始学 Web 之 BOM(二)定时器
  18. mui 列表项左右滑删除功能升级(仿微信左滑 点击删除后出现确认删除)
  19. [paper]MaskFusion: Real-Time Recognition, Tracking and Reconstruction of Multiple Moving Objects
  20. HeaderExchangeClient

热门文章

  1. Linux(CentOS7)下远程拷贝文件,scp命令
  2. 用java命令重新签名apk
  3. RobotFramework和Eclipse集成-安装和使用说明
  4. batch 常用命令
  5. Spire.XLS,生成Excel文件、加载Excel文件
  6. js创建对象,放进js集合
  7. 分布式存储ceph——(5)ceph osd故障硬盘更换
  8. Educational Codeforces Round 61 (Rated for Div. 2)-C. Painting the Fence 前缀和优化
  9. 腾讯通信云服务端使用心得,腾讯云IM
  10. SpringCloud-Ribbon服务调用(三)