#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 being 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) << ' + ';
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 A SL 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 being 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 are :";
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<< "Choose a method to solve the address conflict:";
cin>>choice;
System ("CLS");
Switch (choice)
{
Case 1:F1 ();
Case 2:F2 ();
default:cout<< "Input error!" <<endl;break;
}
}
Hash lookup The two most common methods of resolving address conflicts (linear probing and hashing, chain address method) C + + implementation