hashtable — C ++ Hash Table с ошибкой цепочки

По какой-то причине мой код не работает должным образом. Он компилируется, но когда он выходит на экран вывода, он просто говорит: «Произошла ошибка». Это не перечисляет никаких видов ошибок. Я пытался найти ошибку в своем коде некоторое время, но я не могу найти, где она. Вот мой код:

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

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

Другие решения

Других решений пока нет …