I have a problem with implementing open addressing hash table that uses linear probing. I suspect that the problem is with the function called scan_for. Code::Blocks returns -10737741819 (0xC0000005).
const int table_size = 10;
class HashTable
{
public:
int key;
int value;
HashTable(int key, int value)
{
this->key = key;
this->value = value;
}
};
class DelNode:public HashTable
{
private:
static DelNode *en;
DelNode():HashTable(-1,-1){}
public:
static DelNode *getNode()
{
if (en==NULL)
{
en = new DelNode();
}
return en;
}
};
DelNode *DelNode::en = NULL;
class HashMapTable
{
private:
HashTable **ht;
public:
HashMapleTable()
{
ht = new HashTable* [table_size];
for (int i=0; i<table_size; i++)
{
ht[i] = NULL;
}
}
int HashFunction(int k)
{
return k%table_size;
}
int scan_for(int k)
{
int index = k%table_size;
int deleted = -1;
int i = index;
while (ht[i]!=NULL)
{
if (ht[i]==DelNode::getNode())
{
if (deleted==-1) deleted = i;
}
else if (ht[i]->value==k) return i;
i = (i+1)%table_size;
if (i==index) return deleted;
}
if (deleted!=-1) return deleted;
return i;
}
void insert(int x)
{
int i = scan_for(x);
if (i!=-1) ht[i] = new HashTable(i,x);
}
void remove(int x)
{
int i = scan_for(x);
if (i!=-1)
{
delete ht[i];
ht[i] = DelNode::getNode();
}
}
~HashMapTable()
{
delete[] ht;
}
Ht is hash table that contains keys and values. Function scan_for is a helper function that is used in function such as insert, remove, delete. It searches correct key using linear probing and returnes it, or -1 if it is deleted.
User contributions licensed under CC BY-SA 3.0