Based on the data element's keyword and hash function to establish a hash table and initialize the hash table, using open addressing method to deal with the conflict, according to the screen output of the function table to select the desired function to implement a hash table on the data element insertion, display, find, delete.
Set elem[maxsize], Elemflag[maxsize], and count to 0 when initializing the hash table. When creating a hash table, a hash function is created to create a hash table, input data element keywords, end the input with "0" and require the keyword to be a positive integer, the number of data elements is not allowed to exceed the table length maxsize.
Output form: Outputs the appropriate prompt statement and the correct result based on the function of the selected hash table.
The function of the program: a set of data elements not exceeding the length of the hash table, by their keywords and hash function into the Hashtable, if there is a conflict with open addressing method processing and find the corresponding address. It can be used to insert, display, find, delete data elements with a hash table.
Test data: maxsize=10
hash function: H (key) =key%7
Conflict Resolution Method: Open addressing Method hi= (H (key) +di)%13 i=1,2,3,..., 9
Hash tables are also known as hash lists. The basic idea of the hash table storage is that the value of the function is computed by a function h (k), using the keyword K of each record in the data table as the argument. This value is interpreted as a contiguous storage space (that is, the array space) of the cell address (the subscript), the record is stored in this unit. This is called the function h as a hash function or a hash function. A table created in this way is called a hash table or a hash list.
Ways to handle Conflicts:
Open Addressing Method : hi= (H (key) + di) MOD m, i=1,2,..., K (k<=m-1), where H (key) is a hash function, M is a hash table length, and di is an incremental sequence, the following three methods can be used:
1.di=1,2,3,..., m-1, called linear detection and re-hashing;
2.di=1^2, (-1) ^2, 2^2, (-2) ^2, (3) ^2, ..., ± (k) ^2, (K<=M/2) Two-time detection and re-hashing;
3.di= pseudo random number sequence, called pseudo-random detection re-hash.
re-hashing : Hi=rhi (Key), i=1,2,..., K. RHi are different hash functions, that is, when a synonym generates an address conflict, computes another hash function address, until the conflict no longer occurs, this method is not easy to generate "aggregation", but increase the calculation time;
chain Address Method (Zipper method): Store all keywords as synonyms in the same linear list;
Create a public overflow area .
#include <iostream>
using namespace Std;
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
#define SUCCESS 1
#define UNSUCCESS 0
typedef struct
{int elem[maxsize];
int elemflag[maxsize];
int count;
}hashtable;
void Initialhash (HashTable &h)/* Hash Table initialization */
{int i;
h.count=0;
for (i=0;i<maxsize;i++)
{h.elem[i]=0;
h.elemflag[i]=0;
}
}
int hash (int kn)/* Hash function H (key) =key MOD 7*/
{return (kn%7);
}
int Searchhash (HashTable &h,int k)/* Find the element with the keyword K */
{int t,s;
S=t=hash (k);
Xun:if (h.elem[t]==k&&h.elemflag[t]==1) return SUCCESS;
else if (h.elem[t]!=k&&h.elemflag[t]==1)
{t= (t+1)%maxsize;
Goto Xun;
}
else return unsuccess;
}
int Inserthash (HashTable &h,int e)/* Insert Element e*/
{int p;
P=hash (e);
if (Searchhash (h,e))
{cout<< "already has this number! "<<endl;
return unsuccess;
}
Else
{h.elemflag[p]=1;
H.elem[p]=e;
h.count++;
return SUCCESS;
}
}
void CreateHash (HashTable &h)/* Create hash Table */
{int S;
int e;
cout<< "Please enter a hash table: (Enter 0 to end!) ) "<<endl;
cin>>e;
while (e)
{S=inserthash (h,e);
if (!s)
{cout<< "This number already exists! ";
cin>>e;
}
Else
cin>>e;
}
}
void Printhash (HashTable H)/* display element and its location */
{cout<< "hash table address:";
int i;
for (i=0;i<maxsize;i++)
cout<<i<< "";
cout<<endl<< "keywords:";
for (i=0;i<maxsize;i++)
cout<
cout<<endl<< "keyword flag:";
for (i=0;i<maxsize;i++)
cout<
}
int Deletehash (HashTable &h,int e)/* Delete element e*/
{int i;
int a=0;
for (i=0;i<maxsize;i++)
if (h.elem[i]==e&&h.elemflag[i]==1)
{h.elemflag[i]=2;
h.count--;
h.elem[i]=0;
a++;
return SUCCESS;
}
if (!a)
{cout<< "no such number! "<<endl;
return 0;//return unsuccess;
}
return 0;
}
void Main ()
{HashTable H;
int m,k,p;
int R;
do{cout<<endl<< "\t\t****************** Please select function ********************" <<endl;
cout<< "\t\t\t1. Initialize hash table" <<endl<< "\t\t\t2. Create hash table" <<endl<< "\t\t\t3. Find"
<<endl<< \t\t\t4. Insert <<endl<< \t\t\t5. Delete <<endl<< \t\t\t6. Output hash Table: "<< endl<< "\t\t\t0. Exit" <<endl;
cout<< "\t\t************************************************" <<endl;
cin>>m;
Switch (m)
{Case 1:initialhash (H);
Case 2:createhash (H);
Case 3:cout<<endl<< "Please enter keywords to look for:";
cin>>k;
P=searchhash (H,K);
if (p) cout<< "find success! "<<endl;
Else cout<< "Lookup failed! "<<endl;
Break
Case 4:cout<<endl<< "Please enter the keyword to insert:";
cin>>r;
P=inserthash (H,R);
if (p) cout<< "Insert succeeded! "<<endl;
else cout<< "Insert failed! "<<endl;
Break
Case 5:cout<< "Please output the keyword to delete:";
cin>>r;
P=deletehash (H,R);
if (p) cout<< "Delete succeeded! "<<endl;
else cout<< "Delete failed! "<<endl;
Break
Case 6:printhash (H);
Case 0:break;
default:cout<<endl<< "Choice Error! "; break;
}
}while (m!=0);
}
A hash table for linear detection and re-hashing