带链的基本哈希表

时间:2018-09-26 00:23:52

标签: c++ hashtable

我正准备接受一些采访,因此我将重新编写一个基本的哈希表,并从头开始进行链接。我收到以下错误:

Exception thrown: read access violation.
hashTable was 0x1110116.

在这一行:

auto entry = hashTable[hashValue];

在插入函数中。我检查并重新检查了我的代码,但看不到问题所在。这是完整的代码:

#include <iostream>

const int size = 128;

struct Node {
    int key;
    int value;
    Node* next = nullptr;

    Node(const int& x, const int& y, Node* next = nullptr) : 
        key(x),
        value(y),
        next(next) {}
};
Node** hashTable = nullptr;


void print() {
    Node* temp;
    for (int i = 0; i < size; i++) {
        temp = hashTable[i];
        std::cout << "Key: " << temp->key << " Value: " << temp->value << " ";
    }
    std::cout << "\n";
}

int hash(int key) { return key % size; }

void insert(int key, int value) {
    int hashValue = hash(key);
    Node* previous = nullptr;
    auto entry = hashTable[hashValue];

    while (entry != nullptr) {
        previous = entry;
        entry = entry->next;
    }

    if (entry == nullptr) {
        entry = new Node(key, value);
        if (previous = nullptr) {
            hashTable[hashValue] = entry;
        }
        else {
            previous->next = entry;
        }
    }
    else {
        entry->value = value;
    }
}

void remove(int key) {
    int hashValue = hash(key);
    Node* previous = nullptr;
    auto entry = hashTable[hashValue];

    while (entry != nullptr && entry->key != key) {
        previous = entry;
        entry = entry->next;
    }

    if (entry == nullptr) {
        std::cout << "Key not found" << "\n";
        return;
    }
    else {
        if (previous == nullptr) {
            hashTable[hashValue] = entry->next;
        }
        else {
            previous->next = entry->next;
        }
        delete entry;
    }
}

void search(int key) {
    bool flag = false;
    int hashValue = hash(key);
    auto entry = hashTable[hashValue];

    while (entry != nullptr) {
        if (entry->key == key) {
            std::cout << entry->value << " ";
            flag = true;
        }
        entry = entry->next;
    }
    if (!flag) std::cout << "No key found" << "\n";
}


int main() {

    insert(1, 1);
    insert(2, 2);
    insert(3, 3);
    insert(4, 4);
    print();

    remove(2);
    print();

    search(3);


}

1 个答案:

答案 0 :(得分:1)

代码使用hashTable而不为其内容分配任何空间。这是未定义的行为,肯定会崩溃。我刚刚制作的是一个长度为'size'的指针数组。您还可以使用新的或更好的矢量。

编译器还警告当==被使用时应使用=。

print()函数将打印nullptr导致崩溃。我加了支票。

您几乎拥有了!

#include <iostream>

const int size = 128;

struct Node {
    int key;
    int value;
    Node* next = nullptr;

    Node(const int& x, const int& y, Node* next = nullptr) : 
        key(x),
        value(y),
        next(next) {}
};
Node* hashTable[size]; // give the hash table some space


void print() {
    Node* temp;
    for (int i = 0; i < size; i++) {
        temp = hashTable[i];
        if(temp) // don't try to print of temp == nullptr
            std::cout << "Key: " << temp->key << " Value: " << temp->value << " " << std::endl;
    }
    std::cout << "\n";
}

int hash(int key) { return key % size; }

void insert(int key, int value) {
    int hashValue = hash(key);
    Node* previous = nullptr;
    auto entry = hashTable[hashValue];

    while (entry != nullptr) {
        previous = entry;
        entry = entry->next;
    }

    if (entry == nullptr) {
        entry = new Node(key, value);
        if (previous == nullptr) {
            hashTable[hashValue] = entry;
        }
        else {
            previous->next = entry;
        }
    }
    else {
        entry->value = value;
    }
}

void remove(int key) {
    int hashValue = hash(key);
    Node* previous = nullptr;
    auto entry = hashTable[hashValue];

    while (entry != nullptr && entry->key != key) {
        previous = entry;
        entry = entry->next;
    }

    if (entry == nullptr) {
        std::cout << "Key not found" << "\n";
        return;
    }
    else {
        if (previous == nullptr) {
            hashTable[hashValue] = entry->next;
        }
        else {
            previous->next = entry->next;
        }
        delete entry;
    }
}

void search(int key) {
    bool flag = false;
    int hashValue = hash(key);
    auto entry = hashTable[hashValue];

    while (entry != nullptr) {
        if (entry->key == key) {
            std::cout << entry->value << " ";
            flag = true;
        }
        entry = entry->next;
    }
    if (!flag) std::cout << "No key found" << "\n";
}


int main() {

    insert(1, 1);
    insert(2, 2);
    insert(3, 3);
    insert(4, 4);
    print();

    remove(2);
    print();

    search(3);


}