Ошибка в 134 строке при двойном хешировании
Выполнял задание с двойным хешированием, но выскакивает ошибка в 134 строке. Как решить эту проблему?
#include <iostream>
#include <cstdlib>
#define MIN_TABLE_SIZE 10
using namespace std;
enum EntryType { Legitimate, Empty, Deleted };
struct HashNode // Узел
{
int element;
enum EntryType info;
};
struct HashTable // Таблица
{
int size;
HashNode* table;
};
int HashFunc1(int key, int size) // Первый хеш
{
return key % size;
}
int HashFunc2(int key, int size) //Второй хеш
{
return (key * size - 1) % size;
}
HashTable* initializeTable(int size) // инициализация таблицы
{
HashTable* htable;
if (size < MIN_TABLE_SIZE)
{
cout << "Размер таблицы слишком маленький!" << endl;
return NULL;
}
htable = new HashTable;
if (htable == NULL)
{
cout << "Не хватает места!" << endl;
return NULL;
}
htable->size = size;
htable->table = new HashNode[htable->size];
if (htable->table == NULL)
{
cout << "Рамзер таблицы слишком маленький!" << endl;
return NULL;
}
for (int i = 0; i < htable->size; i++)
{
htable->table[i].info = Empty;
htable->table[i].element = NULL;
}
return htable;
}
int Find(int key, HashTable* htable) // Поиск элемента в таблице
{
int hashVal = HashFunc1(key, htable->size);
int stepSize = HashFunc2(key, htable->size);
while (htable->table[hashVal].info != Empty &&
htable->table[hashVal].element != key)
{
hashVal = hashVal + stepSize;
hashVal = hashVal % htable->size;
}
return hashVal;
}
void Insert(int key, HashTable* htable) //Вставка элемента в таблицу
{
int pos = Find(key, htable);
if (htable->table[pos].info != Legitimate)
{
htable->table[pos].info = Legitimate;
htable->table[pos].element = key;
}
}
HashTable* Rehash(HashTable* htable) // Перехеширование таблицы
{
int size = htable->size;
HashNode* table = htable->table;
htable = initializeTable(2 * size);
for (int i = 0; i < size; i++)
{
if (table[i].info == Legitimate)
Insert(table[i].element, htable);
}
free(table);
return htable;
}
void Retrieve(HashTable* htable) // Получение таблицы
{
for (int i = 0; i < htable->size; i++)
{
int value = htable->table[i].element;
if (!value)
cout << "Position: " << i + 1 << " Element: Null" << endl;
else
cout << "Position: " << i + 1 << " Element: " << value << endl;
}
}
int main() //Меню
{
setlocale(LC_ALL, "Russian");
int value, size, i = 1;
int choice;
HashTable* htable;
while (1)
{
cout << "\n----------------------" << endl;
cout << "Двойное хеширование" << endl;
cout << "\n----------------------" << endl;
cout << "1.Инициализировать размер таблицы" << endl;
cout << "2.Вставить элемент в таблицу" << endl;
cout << "3.Показать хеш-таблицу" << endl;
cout << "4.Перехешировать таблицу" << endl;
cout << "5.Выход" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Введите размер хеш-таблицы: ";
cin >> size;
htable = initializeTable(size);
break;
case 2:
if (i > htable -> size) >Вот тут ошибка: использование неинициализированной памяти htable
{
cout << "Таблица заполнена, перехешировать таблицу" << endl;
continue;
}
cout << "Выберите элемент для вставки: ";
cin >> value;
Insert(value, htable);
i++;
break;
case 3:
Retrieve(htable);
break;
case 4:
htable = Rehash(htable);
break;
case 5:
exit(1);
default:
cout << "\nEnter correct option\n";
}
}
return 0;
}