I need to write a menu controlled program using classes and arrays and integer data to
1) create a hash table (i.e. mark all the elements in the table as "EMPTY")
2) clear a hash table of its contents
3) add data to the hash table (this code is below)
4) remove data from the hash table (also print each value as it is removed),
5) print the contents of the hash table,
6) search the hash table for some target key value (this code is below)
I have been searching the internet and books for a week looking for simple code for the functions above and everything I find is to complicated or its just explaining the maths behind the hash table.
I was given some code to start with but I'm getting errors:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
//exType.cpp - implementation of exType.h, where functionality of ExType obj's are defined
#include"exType.h"
ExType::ExType()
{
idNum=0;
}
ExType::ExType(int id)
{
idNum=id;
}
istream& operator>>(istream &is, ExType &item)
{
is>>item.idNum;
return is;
}
ostream& operator<<(ostream &os, ExType item)
{
os<<item.idNum;
return os;
}
bool ExType::operator==(const ExType& item) const
{
if(idNum == item.idNum)
return true;
else
return false;
}
bool ExType::operator!=(const ExType& item) const
{
if(idNum != item.idNum)
return true;
else
return false;
}
ExType::~ExType()
{
}
int ExType::Hash() const
//Post: Function value is an integer between 0 // and MAX_ITEMS-1
{
return (idNum % MAX_ITEMS);
}
template<class ItemType>
void ListType<ItemType>::InsertItem(ItemType item)
// Post: item is stored in the array at position
//item.Hash() or the next free spot.
{
int location;
location = item.Hash();
while (info[location] != emptyItem && info[location] != deletedItem)
location = (location + 1) % MAX_ITEMS;
info[location] = item;
length++;
}
template<class ItemType>
void ListType<ItemType>::RetrieveItem(ItemType& item, bool& found)
{
int location;
int startLoc;
bool moreToSearch = true;
startLoc = item.Hash();
location = startLoc;
do
{
if (info[location] == item ||
info[location] == emptyItem)
moreToSearch = false;
else
location = (location + 1) % MAX_ITEMS;
} while(location != startLoc && moreToSearch);
found = (info[location] == item);
if ( found)
item = info[location];
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//exType.h defines the item saved sorted and searched in the Hash table.
#ifndef EXTYPE
#define EXTYPE
#include<iostream.h>
const int MAX_ITEMS = 11;
class ExType{
private:
int idNum;
public:
ExType();
ExType(int);
int Hash()const;
friend istream& operator>>(istream &is,ExType &item);
friend ostream& operator<<(ostream &os,ExType item);
bool operator==(const ExType& item)const;
bool operator!=(const ExType& item)const;
~ExType();
void ListType<ItemType>::InsertItem(ItemType item);
void ListType<ItemType>::RetrieveItem(ItemType& item, bool& found);
};
#endif
|
and main.cpp just contains the menu(no problems with this part).
I have just two functions so far and I really don't know what to do for the others.
I don't have to use the code I was given (so if someone has an easier way to do this please let me know bacause I don't really understand the above code and other codes I have looked at seem nothing like this). I understand how hash tables work but I really don't know anything about the code for it. Could someone please help me with this as I am completely lost at what to do.
Thanks