C++ problem, We are to implement an associative array class, with a hash functio
ID: 3756696 • Letter: C
Question
C++ problem, We are to implement an associative array class, with a hash function , receiving input as a contact list for example
so the name would go into the key and the phone number into the data. We are not to use the STD libraries, below is my code but im getting a segmentation fault in the void insert function , how do i correctly implement the associatve array class for the above inputs?
class DataItem {
string data;
string key;
public:
string getdata()const {
return data;
}
DataItem* hashArray[SIZE];
DataItem* dummyItem;
DataItem* item;
DataItem(){
DataItem* hashArray[SIZE];
}
//CRC algorithm: hash function algorithm
int hashCode(string key){ // input, a key (string)
int i, m =31;
unsigned int x= 0,hi; //x = 0, hi = 0
for (i=0; i < key.length();i++){ //for i = 0 to key.length()-1:
hi = x & MASK32; //hi = bitwise-and the most significant 5 bits of x
x<<= 5; //left shift x by 5 bits
x ^= (hi >> 27); //x = hi right-shifted by 27 bits and xor'ed with x
x ^= key[i]; // x = key[i] xor'ed with xor
}
return (x% m); //return x mod array capacity, output, a hashed value (integer)
}
DataItem *search(string key) {
int hashIndex = hashCode(key); //get hash
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
void insert(string key,string data) {
DataItem *item = new DataItem;
item->data = data; // seg fault
item->key = key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty or deleted cell
while(hashArray[hashIndex] != NULL){//}) && hashArray[hashIndex]->key != -1) {
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
DataItem* erase( DataItem* item) {
string key = item->key;
//get the hash
int hashIndex = hashCode(key);
//move in array until an empty
while(hashArray[hashIndex] != NULL) {
if(hashArray[hashIndex]->key == key) {
struct DataItem* temp = hashArray[hashIndex];
//assign a dummy item at deleted position
hashArray[hashIndex] = dummyItem;
return temp;
}
//go to next cell
++hashIndex;
//wrap around the table
hashIndex %= SIZE;
}
return NULL;
}
};
Explanation / Answer
hi i have provided you new Implemented Source code
Implemented C++ Source code:-
======================
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
const int TABLE_SIZE = 29;
class HashEntry
{
public:
int value;
public:
HashEntry(int value)
{
this->value = value;
}
int GetValue()
{
return value;
}
};
class HashMap
{
public:
HashEntry **table;
public:
HashMap()
{
table = new HashEntry*[TABLE_SIZE];
for(int i=0;i<TABLE_SIZE;i++)
{
table[i]=NULL;
}
}
//Destructor to Deallocate the Memory Allocated Each and Every Key value Pair
public:
~HashMap()
{
for(int i=0;i<TABLE_SIZE;i++)
{
if(table[i] != NULL)
delete table[i];
delete[] table;
}
}
public:
// Generates the hash for this table
int HashFunc(int value)
{
return value % TABLE_SIZE;
}
public:
void Insert(int value)//Insert the Item
{
int hash = HashFunc(value);
while (table[hash] != NULL && table[hash]->GetValue() != value)
{
hash=HashFunc(hash+1);
}
if(table[hash]!=NULL)
delete table[hash];
table[hash] = new HashEntry(value);
}
public:
int Search(int value)
{
int hash = HashFunc(value);
while (table[hash] != NULL && table[hash]->GetValue() != value)
{
hash = HashFunc(hash + 1);
}
if (table[hash] == NULL)
return -1;
else
return hash;
}
// Removes a specified value from the table, if the value is found
public:
void Remove(int value)
{
int hash = HashFunc(value);
while (table[hash] != NULL)
{
if (table[hash]->GetValue() == value)
break;
hash=HashFunc(hash + 1);
}
if(table[hash]==NULL)
{
cout<<" Element "<<value<<" is not Found ";
return;
}
else
{
delete table[hash];
}
cout<<"Element Deleted Successfully ";
}
public:
void print()
{
for(int i=0; i < TABLE_SIZE; i++)
{
if (table[i] != NULL)
cout<< table[i]->GetValue()<<"->";
}
cout<<endl;
}
};
int main()
{
HashMap obj;
int value;
int temp_key;
int option;
while (1)
{
cout<<" -------------------------------"<<endl;
cout<<"****Hash Table Operations ****"<<endl;
cout<<"-------------------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the value"<<endl;
cout<<"3.Delete element at a value"<<endl;
cout<<"4.Print elements in hashTable"<<endl;;
cout<<"5.Exit"<<endl;
cout<<"Please Select your choice:"<<endl;
cin>>option;
switch(option)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
temp_key = obj.HashFunc(value);
cout<<"The Element is inserted at the Index or Key is:"<<temp_key<<endl;
obj.Insert(value);
break;
case 2:
cout<<"Enter value of the element to be searched: ";
cin>>value;
if (obj.Search(value) == -1)
{
cout<<"The element "<<value<<" is Not found"<<endl;
continue;
}
else
{
cout<<"Element found at index: ";
cout<<obj.Search(value)<<" ";
}
break;
case 3:
cout<<"Enter value of the element to be deleted: ";
cin>>value;
obj.Remove(value);
break;
case 4:
cout << "The current table is:"<<endl;
obj.print();
break;
case 5:
exit(1);
default:
cout<<" Invalid Option please Enter correct option"<<endl;
}
}
return 0;
}
Sample Output:-
============
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.