注:请注意对应题号,以下内容仅为个人代码,不代表最终答案,仅供参考。
目录
DC04PE35在链地址哈希表中查找关键字,并记录查找过程中发生的冲突次数
DC04PE04有序输出哈希表中的所有关键字
void PrintKeys(HashTable ht, void(*print)(StrKeyType)){
/* 请调用形参中的print函数输出关键字 */
for (char c = 'A'; c <= 'Z'; c++) {
int n = (c - 'A') % ht.size;
int i = n;
while ((n + 1) % ht.size != i) {
if (-1 != ht.rcd[n].tag && ht.rcd[n].key[0] == c) print(ht.rcd[n].key);
n = (n + 1) % ht.size;
}
}
}
DC04PE15链地址哈希表的构造
int BuildHashTab(ChainHashTab &H, int n, HKeyType es[])
{ // Add your code here
if(n==0) return 0;
int num, k;
HLink temp;
H.rcd = (HLink*)malloc(H.size * sizeof(HLink));
H.count = 0;
for (int i = 0; i < H.size; i++) H.rcd[i] = NULL;
for (int i = 0; i < n; i++){
k = 0;
num = Hash(H, es[i]);
temp = H.rcd[num];
if(temp){
k = 1;
while(k){
if (temp->data == es[i]) {
H.count++;
break;
}
k = Collision(H,temp);
}
}
if(!k){
temp= (HNode*)malloc(sizeof(HNode));
temp->data = es[i];
temp->next = H.rcd[num];
H.rcd[num]=temp;
H.count++;
}
}
return 1;
}
DC04PE30计算链地址哈希表中的冲突次数
int countConflics(LHashTable H)
{
int res=0;
for (int i = 0; i < H.size; i++) {
Node *node = H.rcd[i];
int p=0;
while(node){
if(p==0) p=1;
else res++;
node = node->next;
}
}
return res; //Temporary code. Modify it if necessary.
}
DC04PE35在链地址哈希表中查找关键字,并记录查找过程中发生的冲突次数
(这道题测评机有BUG,多提交几次就能通过了)
Node* searchLHash(LHashTable H, KeyType key, int &c)
{
Node *node = NULL;
c=0;
int res=0;
for(int i=0;i<H.size;i++) {
int temp = 0;
res=0;
node = H.rcd[i];
while(node){
if(temp == 0) temp=1;
else res++;
if(node->r.key == key) {
c = res;
return node;
}
node = node->next;
}
node = NULL;
}
return NULL; //Temporary code. Modify it if necessary.
}