带链接错误的C ++哈希表

时间:2014-03-26 20:56:15

标签: c++ hashtable chaining chain

由于某种原因,我的代码运行不正常。它确实编译但是当它进入输出屏幕时,它只是说"发生错误"。它没有列出任何类型的错误。我已经尝试在我的代码中找到错误一段时间,但我似乎无法找到它的位置。这是我的代码:

class Node
{
private:
    int key;
    int value;
    Node* next;
public:
    Node();
   ~Node();
    void setKey(int num);
    int getKey();
    void setValue(int x);
    int getValue();
    void setNext(Node* theNext);
    Node* getNext();
};

Node::Node()
{
    next = NULL;
}

Node::~Node()
{

}

void Node::setKey(int num)
{
    key = num;
}

int Node::getKey()
{
    return key;
}

void Node::setValue(int x)
{
        value = x;
}

int Node::getValue()
{
    return value;
}

void Node::setNext(Node* theNext)
{
    next = theNext;
}

Node* Node::getNext()
{
    return next;
}

const int SIZE = 7;

class chainTable
{
private:
    Node* data[SIZE];
public:
    chainTable();
    ~chainTable();
    int chainHash(int num);
    void insert(int key, int value);
    void printTable();
};

chainTable::chainTable()
{
    for ( int i = 0; i < SIZE; i++ )
    {
        data[i]->setValue(-1);
    }
}

chainTable::~chainTable()
{

}

int chainTable::chainHash(int num)
{
    return num % 5;
}

void chainTable::insert(int key, int value)
{
    if (data[key]->getValue() == -1)
    {
        data[key] = new Node;
        data[key]->setValue(value);
        data[key]->setKey(key);
        data[key]->setNext(NULL);
    }
    else
    {
        Node* temp = data[key];

        while (temp->getNext() != NULL)
        {
            temp = temp->getNext();
        }

        Node* newEntry = new Node();
        newEntry = temp->getNext();

        newEntry->setKey(key);
        newEntry->setValue(value);
        newEntry->setNext(NULL);        
    }
}

void chainTable::printTable()
{
    for ( int i = 0; i < SIZE; i++ )
    {
        Node* temp = data[i];

        while (temp->getNext())
        {
            cout << temp->getValue();
        }
    }
}

int main()
{
    chainTable theChain;

    int arr[7] = {1,2,5,7,7,9,1};

    for ( int i = 0; i < 7; i++ )
    {
        int arrHash = theChain.chainHash(arr[i]);
        theChain.insert(arrHash,arr[i]);
    }

    return 0;
}

1 个答案:

答案 0 :(得分:0)

错误在构造函数和insert中:没有Node个对象,因此无法设置value。我也改变了printTable。 我没有尝试过,但是这样做会更好:

class Node
{
private:
    int key;
    int value;
    Node* next;
public:
    Node();
   ~Node();
    void setKey(int num);
    int getKey();
    void setValue(int x);
    int getValue();
    void setNext(Node* theNext);
    Node* getNext();
};

Node::Node()
{
    next = NULL;
}

Node::~Node()
{

}

void Node::setKey(int num)
{
    key = num;
}

int Node::getKey()
{
    return key;
}

void Node::setValue(int x)
{
        value = x;
}

int Node::getValue()
{
    return value;
}

void Node::setNext(Node* theNext)
{
    next = theNext;
}

Node* Node::getNext()
{
    return next;
}

const int SIZE = 7;

class chainTable
{
private:
    Node* data[SIZE];
public:
    chainTable();
    ~chainTable();
    int chainHash(int num);
    void insert(int key, int value);
    void printTable();
};

chainTable::chainTable()
{
    for ( int i = 0; i < SIZE; i++ )
    {
        //data[i]->setValue(-1);
        data[i] = 0;
    }
}

chainTable::~chainTable()
{
    // ToDo: delete all nodes
}

int chainTable::chainHash(int num)
{
    return num % 5;
}

void chainTable::insert(int key, int value)
{
    if (data[key] == NULL)
    {
        data[key] = new Node;
        data[key]->setValue(value);
        data[key]->setKey(key);
        data[key]->setNext(NULL);
    }
    else
    {
        Node* temp = data[key];

        while (temp->getNext() != NULL)
        {
            temp = temp->getNext();
        }

        Node* newEntry = new Node();
        newEntry = temp->getNext();

        newEntry->setKey(key);
        newEntry->setValue(value);
        newEntry->setNext(NULL);        
    }
}

void chainTable::printTable()
{
    for ( int i = 0; i < SIZE; i++ )
    {
        Node* temp = data[i];

        while (temp)
        {
            cout << temp->getValue();
            temp = temp->getNext();
        }
    }
}

int main()
{
    chainTable theChain;

    int arr[7] = {1,2,5,7,7,9,1};

    for ( int i = 0; i < 7; i++ )
    {
        int arrHash = theChain.chainHash(arr[i]);
        theChain.insert(arrHash,arr[i]);
    }

    return 0;
}