user3421510 user3421510 - 1 month ago 10
C++ Question

C++ Hash Table with Chaining Bug

For some reason, my code is not running properly. It does compile but when it goes to the output screen, it just says "An error has occurred". It does not list any sorts of errors. I've been trying to find the error in my code for a while but I can't seem to find where it is. Here is my code:

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

Answer

The error is in the constructor and in insert: There are no Node objects, so the value can not be set. I also changed printTable. I didn't try it, but like this it should be better:

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