Hash Table

This is a homework assignment. I am not looking for someone to do this for me I am just looking for some help. For some reason I am unable to set my status_arr in add() to 1. I am also having difficulty with my Item method which should return the value found for the key, and also my exists method. Neither of these are returning values. Any help is greatly appreciated.

I have the following:

#include <iostream>
#include <string>

using namespace std;

template <class T>
class hashTable
{
private:
int length;
int HTableSize;
T *value_arr; //Will hold Values in an array of zize 10
string *key_arr; // Will hold Names in an array of zize 10
int *status_arr; // Will hold (0 for Empty, 1 for there is data, -1 for deleted)
int hashFunction(string key); // Name Length - 1 % Size of Array

public:
hashTable(int Size = 10); T& operator[] (string);
~hashTable(); //Destructor Working
void add(string key, const T& value);
bool exists(string key);
void clear(); //Working
void remove(string key);
T& item(string key);


};

//-----------------------------------------item---------------------------------
template <class T>
T& hashTable<T>::item(string key)
{
hashFunction(key);
int hashIndex;
hashIndex = hashFunction(key);
return value_arr[hashIndex];

}

//--------------------------------------hashTable-------------------------------template <class T>
hashTable<T>::hashTable(int Size = 10)
{
value_arr = new T[Size];
key_arr = new string[Size];
status_arr = new int[Size];
HTableSize = Size;
for (int i = 0; i < Size; i++)
status_arr[i] = 0;
}
//-------------------------------------hashFunction-----------------------------

template <class T>
int hashTable<T>::hashFunction(string key)
{
return (key.length() - 1) % HTableSize; //hashFunction

}
//-------------------------------------------add--------------------------------

template <class T>
void hashTable<T>::add(string key, const T &value)
{
int hashIndex;
hashIndex = hashFunction(key); //(key.length() - 1) % HTableSize;

if(status_arr[hashIndex] != 1)
{
key_arr[hashIndex] = key;
value_arr[hashIndex] = value;
status_arr[hashIndex] = 1;
}
else
{
if(status_arr[hashIndex] ==1)
{
hashIndex++;
}
else
{
key_arr[hashIndex] = key;
value_arr[hashIndex] = value;
status_arr[hashIndex] = 1;
}
}








}
//----------------------------------------Exists--------------------------------template <class T>
bool hashTable<T>::exists(string key)
{
bool exists;
int hashIndex;
hashFunction(key);
hashIndex = hashFunction(key);
if (status_arr[hashIndex] == 1)
exists = true;
else
exists = false;

return exists;

}
//----------------------------------------Remove--------------------------------template <class T>
void hashTable<T>::remove(string key)
{
int hashIndex;
hashIndex = hashFunction(key); //hashIndex = (key.length() - 1) % HTableSize;
value_arr[hashIndex] = 0;
status_arr[hashIndex] = -1;

}
//----------------------------------------Clear---------------------------------template <class T>
void hashTable<T>::clear()
{
delete[] value_arr;
delete[] key_arr;
delete[] status_arr;

}



//--------------------------------------~hashTable------------------------------template <class T>
hashTable<T>::~hashTable() //destructor will call clear
{
clear();
}


//----------------------------------------Operator Overload---------------------template <class T>
T& hashTable<T>::operator [](string key)
{
int hashIndex;
hashIndex = hashFunction(key); //hashIndex = (key.length() - 1) % HTableSize;
key_arr[hashIndex] = key;
status_arr[hashIndex] = 1;
return value_arr[hashIndex];
}



int main()
{
hashTable<float>obj;
obj.add("Kirby", 10.5);
obj.add("Jared", 7.5);
obj.add("Mary", 5.5);
obj.add("Ted",8.0);
cout << "I've stored for Kirby: " << obj["Kirby"] << endl;

cout << "I've stored for Mary: " << obj["Mary"] << endl;
cout << "I've stored for Jared: " << obj["Jared"] << endl;
//obj.exists("Jared")
obj.item("Kirby");
obj.remove("Ted");
obj.exists("Kirby");
//obj.clear();
//cout << "I've stored for Kirby: " << obj["Kirby"] << endl;
cout << "I've stored for Mary: " << obj["Mary"] << endl;
cout << "I've stored for Jared: " << obj["Jared"] << endl;
cout << "I've stored for Ted: " << obj["Ted"] << endl;




system("PAUSE");
return 0;

}
I have to say, it seems ok to me. I do have some general comments.

1. You should consider using prime numbers as your hash bucket size.
2. Functions taking strings should be const reference, rather than by value.
3. The item function should return const ref T or T, not a ref T.
Topic archived. No new replies allowed.