繁体   English   中英

输入值后,C ++哈希表程序将挂起

[英]C++ Hash Table Program Hangs after Entering a Value

如果您可以按照下面的主要内容操作,我将运行该程序,我可以输入一个整数,它找到下一个质数,然后询问数据。 一旦输入数据一次,程序就会挂起。 似乎处于无限循环或其他状态。 它不会崩溃。 当我暂停它时,它将在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

这是我的链表.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类的属性中有一个... hashTable指针。 它必须是一个链表指针,nop?

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

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

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<int> **table更改为linkedList<int> *table 如果其他任何人都需要此NOW其余工作散列函数的任何指针,请抓紧我。

暂无
暂无

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

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