【问题标题】:C++ Hash Table Program Hangs after Entering a Value输入值后 C++ 哈希表程序挂起
【发布时间】:2014-04-21 22:39:59
【问题描述】:

如果你可以按照下面我的主要操作,我运行程序,我可以输入一个整数,它会找到下一个素数,然后询问数据。一旦我输入数据一次,程序就会挂起。似乎是在一个无限循环,或什么的。它不会崩溃。当我暂停它时,它会在第 256 行显示带有箭头的 read.c 文件。不知道这意味着什么。任何帮助将不胜感激。

我在 hashtable.h 中有以下类和成员函数声明:

#ifndef HASHTABLE_H
#define HASHTABLE_H
#define TRUE        1
#define FALSE       0
#define VERBOSE     0x01
#define NON_VERBOSE 0x02

#include "linkedlist.h"

class hashTable{
public:
    int keys;
    int tableSize;
    linkedList<int> **table;

    hashTable(const int n);
    //~hashTable();

    void hash(int value);
    int search(int value);
    int divisionMethod(int value, int sizeOfTable);
    int midSquareMethod(int value, int sizeOfTable);
    int total();
    void printHashTable();
    int next_prime(int value, char flag);
};

// constructor
hashTable::hashTable(const int n){
    linkedList<int> newList;
    tableSize = next_prime(n, VERBOSE);
    cout << "Table size is: " << tableSize << "\n";     // for debugging
    system("pause");                                    // for debugging
    table = new linkedList<int>*[tableSize];
    for (int i = 0; i < tableSize; i++)
        table[i] = { new linkedList<int> };
}

// Compute the Hash Function and "Hash" element into table
void hashTable::hash(int value){
    table[value % tableSize]->addToHead(value);
    keys++;
    //divisionMethod(midSquareMethod(value, tableSize), tableSize)
}

// Simple Search Function
// Returns the element searched for if found, 0 otherwise
int hashTable::search(int value){
    return(table[value % tableSize]->search(value));
}

// Divsion Method for producing a semi-unique key
int hashTable::divisionMethod(int value, int sizeOfTable){
    int key;
    key = value % sizeOfTable;
    return(key);
}

// Middle Square Method for producing a semi-unique key
int hashTable::midSquareMethod(int value, int sizeOfTable){
    int key;
    key = ((value * value) & 0x0ff0) >> 4;  // pick the middle 8 bits
    return(key);
}

// Returns the total number of keys in the table
int hashTable::total(){
    return(keys);
}

// Print the hash table (for demonstration purposes
void hashTable::printHashTable(){
    int i = 0, valueToPrint;
    while (i < tableSize){
        cout << i << ": ";
        valueToPrint = table[i]->removeFromHead();
        while (valueToPrint != 0){
            cout << valueToPrint << " -> ";
            valueToPrint = table[i]->removeFromHead();
        }
        cout << "|" << endl;
        i++;
    }
}

int hashTable::next_prime(int value, char flag){
    int FOUND = FALSE;
    int n;
    while (!FOUND) {
        for (n = 2; (n * n) <= value && (value % n) != 0; ++n);

        if ((n * n) <= value) {
            if (flag == VERBOSE)
                cout << value << " is divisible by " << n << "\n";
            value++;
        }
        else {
            if (flag == VERBOSE)
                cout << "The next largest prime is " << value << "\n";
            FOUND = TRUE;
        }
    }
    return(value);
}

#endif

这是我的linkedlist.h:

#ifndef LINKEDLIST_H
#define LINKEDLIST_H

#include <iostream>
using namespace std;

template <class TYPE>
class Node{
public:
    TYPE data;
    Node* next;

    // constructor
    Node(TYPE const& x){
        data = x;
        next = NULL;
    }
};

template <class TYPE>
class linkedList{
    //struct Node{
    //  TYPE data;
    //  Node *next;
    //};

public:
    Node<TYPE> *head;
    Node<TYPE> *tail;
    int size;

    // constructor
    linkedList(){
        head = NULL;
        tail = NULL;
        size = 0;
    }
    ~linkedList();

    void addToHead(TYPE value);
    void addToTail(TYPE value);
    TYPE removeFromHead();
    TYPE removeFromTail();
    TYPE search(TYPE searchData);
    TYPE isEmpty();
};

//destructor
template <class TYPE>
linkedList<TYPE>::~linkedList(void){
    while (head){
        Node<TYPE> *temp = head;
        head = head->next;
        delete temp;
    }
}

// Insert an element at the head (start) of the linked list
template <class TYPE>
void linkedList<TYPE>::addToHead(TYPE value){
    Node<TYPE> *newNode = new Node<TYPE>(value);

    if (isEmpty())
        head = newNode;
    else{
        newNode->next = head;
        head = newNode;
    }
}

// Add an element to the tail (end) of the linked list
template <class TYPE>
void linkedList<TYPE>::addToTail(TYPE value){
    Node<TYPE> *newNode = new Node<TYPE>(value);
    Node *tempPtr;

    if(isEmpty()){
        head = newNode;
        tail = newNode;
    }
    else{
        tail->next = newNode;
        tail = tail->next;
    }
}

// Remove an element from start of Linked List
template <class TYPE>
TYPE linkedList<TYPE>::removeFromHead(){
    TYPE tempValue;
    Node<TYPE> *temp;

    if (head){
        tempValue = head->data;
        temp = head;
        if (head == tail)
            head = tail = 0;
        else
            head = head->next;
        delete temp;
        return tempValue;
    }
    else
        return 0;
}

// Remove an element from the end of the linked list
template <class TYPE>
TYPE linkedList<TYPE>::removeFromTail(){
    TYPE tempValue;
    Node *temp;

    if (tail){
        tempValue = tail->data;
        if (head == tail){
            delete head;
            head = tail = 0;
        }
        else{
            for (temp = head; temp->next != tail; temp = temp->next);
            delete tail;
            tail = temp;
            tail->next = 0;
        }
        return tempValue;
    }
    else
        return 0;
}

// Search for an element in the linked list
// Will return the element if found, otherwise it returns 0
template <class TYPE>
TYPE linkedList<TYPE>::search(TYPE searchData){
    Node<TYPE> *temp;

    temp = head;
    while (temp->next != tail){
        if (tail->data == searchData)
            return searchData;
        if (temp->data == searchData)
            return searchData;
        else
            temp = temp->next;
    }
    return 0;
}

// isEmpty() function checks if head == NULL
template <class TYPE>
TYPE linkedList<TYPE>::isEmpty(){
    return(head == NULL);
}

#endif

这是我的主要内容:

#include "hashtable.h"

int main(){
    int n, input;

    cout << "Enter an integer: ";
    cin >> n;
    cout << "\n\n";

    hashTable myHashTable(n);

    cout << "Enter some values into the table:" << endl;
    cin >> input;

    while (input != 0){
        myHashTable.hash(input);
        cin >> input;
    }

    myHashTable.printHashTable();
}

【问题讨论】:

  • 请张贴 hashtable.h
  • 一定有问题,你的 hashTable 类的属性中有一个 hashTable 指针。它必须是一个链表指针,nop ?
  • @AntiClimacus 你应该把它作为答案!
  • 可能与您的问题无关,但table[tableSize] = { 0 }; 可能是个坏主意,因为我看不到table 正在初始化或分配给它的任何内存。
  • @RetiredNinja 实际上我确实在我的程序中对此进行了注释。实际上,我必须将其注释掉,以使程序在我为 tableSize 输入整数时不会崩溃。谢谢。我会在这里更新。

标签: c++ class linked-list hashtable member-functions


【解决方案1】:

一定有问题,你的 hashTable 类的属性中有一个 hashTable 指针。它必须是一个链表指针,nop ?

【讨论】:

  • 我现在意识到这真的不能解决我的问题。我不希望 table 成为链接列表。我希望它是一个名为 table 的数组,用于 hashTable 构造函数和其他函数。有一个更好的方法吗?我要做的是用质数分配我的哈希表的大小,这取决于用户输入的整数。我将编辑我上面的帖子以反映代码。
【解决方案2】:

我确实找到了造成这一切的原因。这就是我在指针数组中实现链表的方式。几乎只是漫长夜晚的程序员错误。当然,我在此处发布的代码有很多错误,我已将其全部修复,例如我的哈希类等中的搜索功能,

这是我所做的更改,几乎解决了我在此处发布的大部分问题:

hashTable::hashTable(const int n, char hFunction){
    keys = 0;
    hashFunction = hFunction;
    tableSize = next_prime(n, VERBOSE);
    cout << "Table size is: " << tableSize << "\n\n";       // for debugging
    system("pause");                                        // for debugging
    table = new linkedList<int>[tableSize];

我还将我的linkedList&lt;int&gt; **table 更改为linkedList&lt;int&gt; *table。如果其他人需要关于这个 NOW 工作哈希函数的其余部分的任何指针,请联系我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-14
    • 2016-08-06
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多