hash.cpp

来自「数据结构算法之一」· C++ 代码 · 共 58 行

CPP
58
字号
// Hash.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Hash.h"

int main(int argc, char* argv[])
{	HashTable H;
	KeyType K="";
	int p,j,c=0;
	char t;
	H.elem=(ElemType *)malloc(TABLE_LEN * sizeof(ElemType));
	cout<<"\t\t\tHASHTABLE ARITHMATIC"<<endl;
	cout<<"(Please Input the total number of HashTable,for example:30):  ";
 cin>>H.count;
for(int i=0;i<TABLE_LEN;i++)
{ strcpy(H.elem[i].key,NULLKEY);
}//初始化为空串
 
for(i=0;i<H.count;i++)
 {
  cout<<"Input name["<<i<<"] : ";
  cin>>K;
 p=Hash(K);
if(strcmp(H.elem[p].key,NULLKEY)!=0)
 collision(H,p,++c);// 双哈希再散列 

 strcpy(H.elem[p].key,K);
 }//初始化赋值


cout<<"=====================Hash Table==========================="<<endl;

for( i=j=0;i<TABLE_LEN;i++)
{if(strcmp(H.elem[i].key,"")!=0){
 if(j%2==0&&j!=0) cout<<endl;
 cout<<"pos: "<<i<<"  key: \""<<H.elem[i].key<<"\"\t";
 j++;
   } 
}
cout<<"collision :"<<c<<endl;
cout<<"=========================================================="<<endl;
//显示哈希表

do{
cout<<"Input the name you want to search: ";
 cin>>K;
 c=0;
 if(SearchHash(H,K,p,c)==SUCCESS){
	 cout<<"pos :"<<p<<"  key :"<<H.elem[p].key<<"   collision :"<<c<<endl;}
 else {
	 cout<<"not find!!!"<<endl;}	
	cout<<"Next to search?Yes(y) or NO?  :";
	cin>>t;
}while(t=='y'||t=='Y');//查找功能
return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?