#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学习资料干货喔

哈希查找解决地址冲突的两种最常见方法(线性探测再散列,链地址法)C++实现的更多相关文章

  1. DS哈希查找--线性探测再散列

    题目描述 定义哈希函数为H(key) = key%11.输入表长(大于.等于11),输入关键字集合,用线性探测再散列构建哈希表,并查找给定关键字. --程序要求-- 若使用C++只能include一个 ...

  2. 哈希表---线性探测再散列(hash)

    //哈希表---线性探测再散列 #include <iostream> #include <string> #include <stdio.h> #include ...

  3. JDK8;HashMap:再散列解决hash冲突 ,源码分析和分析思路

    JDK8中的HashMap相对JDK7中的HashMap做了些优化. 接下来先通过官方的英文注释探究新HashMap的散列怎么实现 先不给源码,因为直接看源码肯定会晕,那么我们先从简单的概念先讲起   ...

  4. GET和POST两种基本请求方法(转自博主--在途中#)

    GET和POST两种基本请求方法的区别 GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL中,POST通过req ...

  5. 转:GET和POST两种基本请求方法的区别

    原文地址:GET和POST两种基本请求方法的区别 原文如下: GET和POST是HTTP请求的两种基本方法,要说它们的区别,接触过WEB开发的人都能说出一二. 最直观的区别就是GET把参数包含在URL ...

  6. 表格细边框的两种CSS实现方法

    在网页制作中,细边框这个制作方法是必不可少的.这里介绍2种常见的表格细边框制作方法,均通过XHTML验证. <!DOCTYPE html PUBLIC "-//W3C//DTD XHT ...

  7. 两种HTTP请求方法:GET和POST的区别

    之前在一些开发者平台使用网页调用API时,一再提到两种请求方法GET和POST,所以就去了解了下.那么这又不得不提到HTTP了! 一.什么是 HTTP? 超文本传输协议(HTTP)的设计目的是保证客户 ...

  8. 修改linux 两种时间的方法

    1,整理了一下怎么修改linux 两种时间的方法. 硬件时间:hwclock 或者clock,设置的方法是 hwclock --set --date="05/12/2018 12:30:50 ...

  9. 哈希表(一):解决hash冲突的几种方法

    (一)线性探测法 线性探测法是最简单的处理冲突的方法. (1)插入元素:插入元素时,如果发生冲突,算法将从该槽位向后遍历哈希表,直到找到表中的下一个空槽,并将该值放入到空槽当中. (2)查找元素:查找 ...

随机推荐

  1. Express (Routing、Middleware、托管静态文件、view engine 等等)

    1. Express 简介 Express 是基于 Node.js 平台,快速.开放.极简的 web 开发框架,它提供一系列强大的特性,帮助你创建各种 Web 和移动设备应用. Express 不对 ...

  2. nyist 20 吝啬的国度(dfs)

    吝啬的国度 题目描述: 在一个吝啬的国度里有N个城市,这N个城市间只有N-1条路把这个N个城市连接起来. 现在,Tom在第S号城市,他有张该国地图,他想知道如果自己要去参观第T号城市, 必须经过的前一 ...

  3. vs2010开发activex(MFC)控件/ie插件(一)

    原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/50782904  vs2010开发activex(MFC)控件:      第一步:生成ac ...

  4. MySQL数据库、表的字符编码

    用MySQL命令行新建数据库和表时默认的字符编码是latin1,但是在实际开发过程中一般都是使用utf8格式的编码.操作如下: 1.修改数据库字符编码 mysql> alter database ...

  5. tomcat启动很慢 停留在 At least one JAR was scanned for TLDs yet contained no TLDs.

    部署项目时候,发现启动特别慢,要等好几分钟,这不正常啊.然后每次部署都停留在 At least one JAR was scanned for TLDs yet contained no TLDs. ...

  6. LeetCode-Container With Most Water-zz

    先上代码. #include <iostream> #include <vector> #include <algorithm> using namespace s ...

  7. Redis 优化之 tcp-backlog

    默认值 511 tcp-backlog:511 此参数确定了TCP连接中已完成队列(完成三次握手之后)的长度, 当然此值必须不大于Linux系统定义的/proc/sys/net/core/somaxc ...

  8. DOS 保存系统日志

    1.导出系统日志,以当前日期命名 @echo offset nowDate=%date%set tmp=%nowDate:~0,-3%set file=%tmp:/=-%系统日志.evtx echo ...

  9. 二、基于事件的异步编程模式(EAP)

    一.引言 在上一个专题中为大家介绍了.NET 1.0中提出来的异步编程模式--APM,虽然APM为我们实现异步编程提供了一定的支持,同时它也存在着一些明显的问题--不支持对异步操作的取消和没有提供对进 ...

  10. Linux-python的一些小问题

    1.python版本和pip版本 2.PATH和PYTONPATH 1.python版本和pip版本 1.python版本 一般Ubuntu里面都装的不止一个版本的python,比如有python2. ...