打开地址哈希表分段错误

时间:2014-10-31 23:35:53

标签: c++ segmentation-fault hashtable

我试图创建一个开放的地址哈希表。用户必须能够定义表的大小。对于我的碰撞解决方案,我正在使用伪随机探测 -

http://algoviz.org/OpenDSA/Books/OpenDSA/html/HashCImproved.html#HashingPseudoRandomProbePRO

无论如何,哈希函数似乎工作正常,直到我只有一个空格来填充表格。一旦我输入表格的最后一个元素,我的程序崩溃,我得到一个分段错误。我一直在拉着我的头发试图找出问题好几个小时。请帮忙!

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;

class HashTable
{

    string* hash;
    bool* inTable;
    vector<int> perm;
    int table_size;
    int permFunc(int, int);
    void permutation();
    int hashFunc(string);

public:

    //constructor for a hashtable
    HashTable(int);

    //deconstructor for hashtable
    ~HashTable();

    bool isFull();

    //The prototype for the insert method
    void insert(string);

     //The prototype for the lookup method
    bool lookup(string);

    //The prototype for the remove method
    void remove(string);

    //The prototype for the size method
    int size();

    //The prototype for the print method
    void print();

};

//constructor

HashTable::HashTable(int table_size)
{
    this -> table_size = table_size;
    hash = new string[table_size];
    inTable = new bool[table_size];
    for(int i=0; i<table_size; i++) perm.push_back(i);
    permutation();
}

//deconstructor
HashTable::~HashTable()
{
    delete [] hash;
    delete [] inTable;
}

//creates a permutation of the perm vectors index elements
void HashTable::permutation()
{
    random_shuffle ( perm.begin() + 1, perm.end() );
}

//Hash function method for strings
int HashTable::hashFunc(string str)
{
    int key = 0;
    for (int i=0; i < str.size(); i++)
    {
        key += static_cast<int>(str.at(i));
    }
    cout << "key is:" << key << endl;
    return key%table_size;
}

int HashTable::permFunc(int index, int i)
{
        int temp = index;
        index += perm[i];
        if(index > table_size){index = index - table_size;}
        if(inTable[index] == false ){return index;}
        index = temp;
        i++;
        permFunc(index, i);
}

bool HashTable::isFull()
{
    int num=0;
    for(int i=0;i<table_size; i++)
    {
        if(inTable[i] == true){num ++;}
        if(num == table_size){
            cout<<"FULL TABLE";
            return true;}
    }
    return false;
}

//Method that inserts an integer into the hash table
void HashTable::insert(string str)
{
    int i=1;
    if(isFull()){return;}

    int pos = hashFunc(str);
    if(inTable[pos] == true)
    {
        int permPos = permFunc(pos, i);
        hash[permPos] = str;
        inTable[permPos] = true;
    }
    else
        hash[pos] = str;
        inTable[pos] = true;
}
//A function that prints out the menu
void menu()
{
    cout << endl;
    cout << "press i to insert an element into the hash table" << endl;
    cout << "press d to inTableete an element from the hash table" << endl;
    cout << "press l to look up an element" << endl;
    cout << "press s to obtain the size of the table" << endl;
    cout << "press p to print the current table" << endl;
    return;
}

//The main method
//Implements all the input and output
int main(void)
{
    int table_size;
    cout << "Enter table size: ";
    cin >> table_size;

    HashTable h(table_size);
    while(true)
    {
        char c;
        string str;
        menu();
        getline(cin, str);
        stringstream stream;
        stream << str;
        stream >> c;
        switch(c)
        {
            case 'i':
                getline(cin, str);
                h.insert(str);
                break;
            case 'd':
                getline(cin, str);
                h.remove(str);
                break;
            case 'l':
                getline(cin, str);
                cout << h.lookup(str) << "\n";
                break;
            case 's':
                cout << h.size() << "\n";
                break;
            case 'p':
                h.print();
                break;
            default:
                cout << "Don't understand '" << c << "'\n";

        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:0)

我不知道这是你的问题,但这一行:

if(index > table_size){index = index - table_size;}

index留在table_size,这超出了您的阵列范围。将>更改为>=

如果不是这样,您是否可以在帖子中添加示例崩溃输入以进行测试?