简体   繁体   English

如何使用除法和二次探测将 int 存储到 hash 表中以避免 C++ 中的冲突

[英]How to store int into hash table using division method and quadratic probing to avoid collision in C++

So recently I had a quiz for my Data Structure subject, and there was a question involving hash tables.所以最近我有一个关于我的数据结构主题的测验,有一个问题涉及 hash 表。 My lecturer did teach the theories behind it but never taught the way to code it which was extremely frustrating.我的讲师确实教过它背后的理论,但从未教过编码方式,这非常令人沮丧。 I tried to google any info on how to do it, but to no avail.我试图在谷歌上搜索有关如何操作的任何信息,但无济于事。 Now after the quiz, I just want to know how this is done so I can improve my knowledge for further tests or quizzes.现在在测验之后,我只想知道这是如何完成的,以便我可以提高我的知识以进行进一步的测试或测验。 I've also added my submission incase anyone needed to see it.我还添加了我的提交内容,以防有人需要查看。

我的数据结构测验

#include <iostream>
using namespace std;

int hash[30]; 
string key;
int value, keylength;
char yesorno;

int division(int keylength){
for(int i = 0; i <= keylength; i++){
    value = keylength % 30;
    hash[value];
    cout << "The bucket/cell location : " << hash[value] << endl;
}
}


/*int hashing(int hash[], int value){
for(int k = 0; k <=30; k++){
    hash[k] = value;
    cout << "The bucket/cell location : " << hash[value] << endl;
}
}*/

int main(){

do {
cout << "Enter planet name : ";
cin >> key;
keylength == key.length();
division(keylength);
//hashing(hash, value);
cout << "Do you want to continue...? " << endl;
cout << "Press 'Y' or 'y' for Yes else 'N' or 'n' : ";
cin >> yesorno;
} while(yesorno = 'Y' || 'y');

return 0;
}

The question is not asking for storing int into hash table but the value of position in hash table where key (here planet names) is stored.问题不是要求将int存储到 hash 表中,而是要求存储key (此处为行星名称)的 hash 表中 position 的值。 Quadratic probing will be implemented somewhat like this:二次探测将像这样实现:

  1. Create hash value from key using division method使用除法从键创建 hash 值

  2. If hashtable[value] is unoccupied then no need of any probing.如果hashtable[value]未被占用,则不需要任何探测。 We occupy it and return value.我们占据它并返回值。 Else we will start quadratic probing like this:否则,我们将像这样开始二次探测:

    for(int i=0;i<30;i++){ probe_value=(i*i+value)%30; // quadratic probing if(hashtable[probe_value] == "0"){ return probe_value; } } return -1; // as hashtabletable is full;

Complete program code:完整的程序代码:

#include<bits/stdc++.h>
using namespace std;
string hashtable[30];
int hashtableing(string key){
        int value,probe_value;
        value= (key.length())%30; //division
        if(hashtable[value] == "0" || hashtable[value] ==key){
                hashtable[value]=key;
                return value;
        }else{
                for(int i=0;i<30;i++){
                        probe_value=(i*i+value)%30; // quadratic probing
                        if(hashtable[probe_value] == "0"){
                                return probe_value;
                        }
                }
                return -1; // as hashtabletable is full;
        }
}


int main(){
        string key;
        char choice='Y';
        for(int i=0;i<30;i++){
                hashtable[i]="0"; //0 indicates cell is unoccupied for probing
        }
        while(choice != 'N' && choice !='n'){
                cout<<"Enter planet name:"<<endl;
                cin>>key;
                cout<<"The bucket/cell location :"<< hashtableing(key)<<endl;
                cout<<"Do you want to continue...?"<<endl;
                cout<<"Press 'Y' or 'y' for Yes else 'n' or 'N':";
                cin>>choice;
        }
        return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM