#include "iostream.h"
typedef int KeyType;
typedef struct{
KeyType key;
//…….;
}ElemType;
typedef int Status;
typedef struct {
ElemType *elem;
int count; // 当前数据元素个数
int sizeindex; // hashsize[sizeindex]为当前容量
} HashTable;
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
void creatHashTable(HashTable &H) {
int i;
H.elem=new ElemType[11];
H.sizeindex=11;
H.count=0;
for( i=0; i<11; i++)
H.elem[i].key=0;
for( i=0; i<9; i++)
cin>>H.elem[i].key;
H.count =9;
}
Status SearchHash (HashTable H, KeyType K,int &P, int &c) {
// 在开放定址哈希表H中查找关键码为K的记录
P=K%11; // 求得哈希地址
while ( H.elem[P].key != 0 &&
K!= H.elem[P].key) {
P=(P+1)%11; c++; // 求得下一探查地址
}
if (K== H.elem[P].key) return SUCCESS;
// 查找成功,返回待查数据元素位置 p
else return UNSUCCESS;
} // SearchHash
//插入记录的函数
insert(HashTable H, KeyType K,int &P)
{
int flag,e;
flag=SearchHash (HashTable H, KeyType K,int &P);
if(!flag)
H.elem[P]=e;
}
void main( ) {
HashTable H;
int key,P,c=0;
int flag;
creatHashTable (H);
cout<<"input key:";
cin>>key;
flag= SearchHash(H, key,P,c);
if(flag)
cout<<"SUCCESS"<<P<<c;
else cout<<"UNSUCESS"<<c;
}
typedef int KeyType;
typedef struct{
KeyType key;
//…….;
}ElemType;
typedef int Status;
typedef struct {
ElemType *elem;
int count; // 当前数据元素个数
int sizeindex; // hashsize[sizeindex]为当前容量
} HashTable;
#define SUCCESS 1
#define UNSUCCESS 0
#define DUPLICATE -1
void creatHashTable(HashTable &H) {
int i;
H.elem=new ElemType[11];
H.sizeindex=11;
H.count=0;
for( i=0; i<11; i++)
H.elem[i].key=0;
for( i=0; i<9; i++)
cin>>H.elem[i].key;
H.count =9;
}
Status SearchHash (HashTable H, KeyType K,int &P, int &c) {
// 在开放定址哈希表H中查找关键码为K的记录
P=K%11; // 求得哈希地址
while ( H.elem[P].key != 0 &&
K!= H.elem[P].key) {
P=(P+1)%11; c++; // 求得下一探查地址
}
if (K== H.elem[P].key) return SUCCESS;
// 查找成功,返回待查数据元素位置 p
else return UNSUCCESS;
} // SearchHash
//插入记录的函数
insert(HashTable H, KeyType K,int &P)
{
int flag,e;
flag=SearchHash (HashTable H, KeyType K,int &P);
if(!flag)
H.elem[P]=e;
}
void main( ) {
HashTable H;
int key,P,c=0;
int flag;
creatHashTable (H);
cout<<"input key:";
cin>>key;
flag= SearchHash(H, key,P,c);
if(flag)
cout<<"SUCCESS"<<P<<c;
else cout<<"UNSUCESS"<<c;
}