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

时间:2014-04-21 22:40:00

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

如果您可以按照下面的主要内容运行程序,我可以输入一个整数,它会找到下一个素数,然后请求数据。一旦我输入数据一次,程序就会挂起。似乎处于一个无限循环或其他东西。它没有崩溃。当我暂停它时,它会在第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();
}

2 个答案:

答案 0 :(得分:1)

有些东西一定是错的,你有hashTable类的属性......一个hashTable指针。它必须是一个链表指针,nop?

答案 1 :(得分:0)

我确实发现了造成这一切的原因。这是我在我的指针数组中实现链表的方式。几乎只是长夜的程序员错误。当然,我在这里发布的代码存在很多问题,我将其全部修复,例如在我的哈希类等中搜索函数,

这是我改变的内容,几乎解决了我在此处发布的问题的很大一部分:

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工作哈希函数的其余部分的任何指针,请抓住我。