【问题标题】:C++ Hash Table with Chaining Bug带有链接错误的 C++ 哈希表
【发布时间】:2014-03-26 20:56:15
【问题描述】:

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

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;
}

【问题讨论】:

    标签: c++ hashtable chaining chain


    【解决方案1】:

    错误在构造函数和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;
    }
    

    【讨论】:

    • 成功了!非常感谢!虽然我改变了它,但我仍然有点困惑为什么它在构造函数中不起作用。我不是创建了 Node 数组吗?节点*数据[大小]。那不是节点对象吗?
    • Node* data[] 是指向节点对象的指针数组。但一开始,没有对象,这给出了错误。在插入函数中,使用new Node,创建了一个对象。
    • 哦,好的。我现在知道了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2012-04-23
    相关资源
    最近更新 更多