#include<iostream>
#include<iomanip>
using namespace std;

typedef struct Node
{
 int data;
 struct Node *next;
}node;

int len,num,M,numbers,i=0,mod,count=0;
int *a;
node **p,*s;
float ASL=0,ASL1=0;

int ListLength(node * head)
{
 int length=0;
 node *p;
 p=head;
 while(p)
 {
  length++;
  p=p->next;
 }
 return length;
}

void Insert(node * &head,node * s)
{
 node *p;
 p=head;
 while(p->next)
 {
  p=p->next;
 }
 p->next=s;
}

void Print(node * head)
{
 node *p;
 p=head;
 while(p)
 {
  cout<<"->"<<p->data;
  p=p->next;
 }
}

void f1()
{
 cout<<"Input HashTable address length:";
 cin>>len;
 cout<<"Input the value of M in Hash function H(k)=k%M:";
 cin>>M;
 a=new int[len];
 cout<<"Input the number of data to be hashed into the hash table:";
 cin>>numbers;
 for(i=0;i<len;i++)
 {
  a[i]=-1;
 }
    cout<<"please input the keys:";
    for(i=1;i<=numbers;i++)
 {
  cin>>num;
  mod=num%M;
  if(a[mod]==-1)
  {
  a[mod]=num;
  ASL+=1;
  }
  else
  {
   while(a[(++mod)%len]!=-1)
   {
    count++;
   }
   a[mod]=num;
   ASL+=(count+2);
  }
 }

for(i=0;i<len;i++)
 {
  count=0;
  if(a[i]==-1)
   ASL1+=0;
  else
  {
   int j=i;
   while(a[(++j)%len]!=-1)
   {
    count++;
   }
   ASL1+=(count+1);
  }
 }
 cout<<"the hash table is as followed:"<<endl<<endl;
 cout<<"HashAddress";
 for(i=0;i<len;i++)
 {
  cout<<setw(4)<<i;
 }
 cout<<endl;
 cout<<"    Key    ";
 for(i=0;i<len;i++)
 {
  if(a[i]==-1)
   cout<<setw(4)<<'\0';
  else
      cout<<setw(4)<<a[i];
 }
 cout<<endl<<endl;
 cout<<"Hash search successfully using linear detection method to resolve conflicts and the average search length ASL is:";
 cout<<(float)ASL/numbers<<endl;
 cout<<"Hash search unsuccessfully using linear detection method to resolve conflicts and the average search length ASL is:";
 cout<<(float)ASL1/len<<endl;
}

void f2()
{
 cout<<"input HashTable address length:";
 cin>>len;
 p=new node*[len];
 cout<<"Input the value of M in Hash function H(k)=k%M:";
 cin>>M;
 cout<<"Input the number of key to be hashed into the hash table:";
 cin>>numbers;
 for(i=0;i<len;i++)
 {
  p[i]=NULL;
 }
 cout<<"please input the keys:";
 for(i=0;i<numbers;i++)
 {
  cin>>num;
  mod=num%M;
  if(p[mod]==NULL)
  {
   p[mod]=new node;
   p[mod]->data=num;
   p[mod]->next=NULL;
   ASL+=1;
  }
  else
  {
   s=new node;
   s->data=num;
   s->next=NULL;
   Insert(p[mod],s);
   ASL+=ListLength(p[mod]);
  }
 }
 for(i=0;i<len;i++)
 {
  if(p[i]==NULL)
   ASL1+=0;
  else
   ASL1+=ListLength(p[i]);
 }
 cout<<"the hash table is as followed:"<<endl<<endl;
 for(i=0;i<len;i++)
 {
  cout<<setiosflags(ios::left)<<setw(3)<<i;
  if(p[i]==NULL)
   cout<<'^'<<endl;
  else
  {
   Print(p[i]);
   cout<<endl;
  }
 }
 cout<<endl;   
 cout<<"Hash search successfully using chain address method to resolve conflicts and the average search length ASL is:";
 cout<<(float)ASL/numbers<<endl;
 cout<<"Hash search unsuccessfully using chain address method to resolve conflicts and the average search length ASL is:";
 cout<<(float)ASL1/len<<endl;
}

void main()
{
    int choice;
 cout<<"1.Linear detection and re hash "<<endl;
 cout<<"2.separate chaining "<<endl;
 cout<<"please choose a method to solve the address conflict:";
 cin>>choice;
 system("cls");
 switch(choice)
 {
 case 1:f1();break;
 case 2:f2();break;
 default:cout<<"input error!"<<endl;break;
 }
}

若有不足欢迎指正;若有疑问鄙人也乐于解答,欢迎留言或QQ加群!

欢迎加入QQ群:735472015,群内有VC,MFC,win32API,批处理,python学习资料干货喔

最新文章

  1. js限制输入框只能输入数字
  2. oracle数据库使用三个月的总结
  3. C#为什么不采用多继承:
  4. 如何利用word2013写图文并茂的博客
  5. Office 2016 正式发布——新特性预览
  6. 14、SEO工程师要阅读的书籍 - IT软件人员书籍系列文章
  7. aop实现日志(转)
  8. UWP深入学习四:动画及图像
  9. PHP 转换接口编码
  10. C#获取程序路径
  11. HDU 4686 矩阵快速幂 Arc of Dream
  12. 作业:用HTML制作邮箱登陆界面
  13. xampp下安装yii框架下遇到的问题
  14. Python isinstance判断对象类型
  15. Hibernate criteria 混合sql语句多表关联时查询注意事项
  16. sql - 选出指定范围的行
  17. [c#]asp.net开发微信公众平台(6)阶段总结、服务搭建、接入
  18. Linux目录和权限
  19. 怎么 得到 DBGrid选中行的数据
  20. Python 内置函数math,random

热门文章

  1. Java的异常处理throw和throws的区别
  2. CentOS使用fdisk扩展磁盘空间
  3. C语言实现整数数组的逆置算法
  4. Office - InfoPath
  5. UEditor 百度富文本编辑器 .Net实例
  6. Android解析ActivityManagerService(二)ActivityTask和Activity栈管理
  7. androidwebview网页显示事件
  8. JAVA读取HDFS信息
  9. 利物浦VS热刺,我努力不去想,但利物浦真的在争冠
  10. Ubuntu16.04使用所遇问题记录