簡體   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