Добавил:
Upload
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include "stdafx.h"
#include <string>
//вам нужно дописать set().get().clear();
#include <iostream>
#include <climits>
#include <clocale>
using namespace std;
template <typename T>
class MyHashTable
{
public:
MyHashTable(const unsigned size);
virtual ~MyHashTable();
void clear();
bool empty();
unsigned size();
unsigned maxSize();
bool get(const string& key, T& value);
string getname(const string& key);
bool set(const string& key, const T value);
void printLog();
private:
class Element
{
public:
Element(const string& key, const T value);
virtual ~Element();
string key;
T value;
Element *next;
};
unsigned mSize; // size of the hash table
unsigned mCount; // number elements which are contained in the hash table
Element **mArray;
unsigned hashFunction(const string& key);
};
template <typename T>
MyHashTable<T>::MyHashTable(const unsigned size)
{
mSize = size;
//Create table of pointers
mArray = new Element*[mSize];
mCount = 0;
for(unsigned i=0; i<mSize; i++)
{
mArray[i]=NULL;
}
}
template <typename T>
MyHashTable<T>::~MyHashTable()
{
if (mArray != NULL)
{
//Clean the hash table
clear();
//Erase table of pointers
delete[] mArray;
mArray = NULL;
}
}
template <typename T>
bool MyHashTable<T>::empty()
{
return (!mSize);
}
template <typename T>
unsigned MyHashTable<T>::size()
{
return mCount;
}
template <typename T>
unsigned MyHashTable<T>::maxSize()
{
return UINT_MAX;
}
template <typename T>
void MyHashTable<T>::printLog()
{
if (mArray != NULL)
{
for (int i = 0; i < mSize; i++)
{
cout << "printLog index " << i << endl;
Element* el = mArray[i];
while(el != NULL)
{
cout << "printLog key = " << el->key;
cout << endl;
el = el->next;
}
}
}
}
template <typename T>
unsigned MyHashTable<T>::hashFunction(const string& key)
{
/*unsigned value = 0;
for(string::const_iterator cit = key.begin(); cit != key.end(); ++cit)
{
value += *cit;
value %= mSize;
}
return value;*/
return key[key.size()-1]%mSize;
}
template <typename T>
MyHashTable<T>::Element::Element(const string& newKey, const T newValue):key(newKey), value(newValue), next(NULL)
{
}
template <typename T>
MyHashTable<T>::Element::~Element()
{
cout << "delete key " << key << endl;
}
template<typename T>
void MyHashTable<T>::clear()
{
Element *del = NULL;
Element *temp = NULL;
for(unsigned i=0;i<mSize; ++i)
{
if(mArray[i] != NULL)
{
del = mArray[i];
if(del->next != NULL)
{
temp = del->next;
while(temp != NULL)
{
delete del;
del=temp;
temp=temp->next;
}
}
delete del;
mArray[i]=temp=del=NULL;
}
}
}
template<typename T>
bool MyHashTable<T>::set(const string& key, const T value)
{
unsigned index = hashFunction(key);
if(mArray[index] != NULL)
{
Element * temp = mArray[index];
while(temp->next != NULL)
temp=temp->next;
Element * elem = new Element(key,value);
temp->next = elem;
return true;
}
mArray[index] = new Element(key,value);
mArray[index]->next = NULL;
return true;
}
template<typename T>
bool MyHashTable<T>::get(const string& key, T& value)
{
unsigned index = hashFunction(key);
Element* el = mArray[index];
while(el != NULL)
{
if (el->key == key)
{
value = el->value;
return true;
}
el = el->next;
}
return false;
}
template<typename T>
string MyHashTable<T>::getname(const string& key)// поиск по ключу
{
unsigned index = hashFunction(key);
string value;
Element* el = mArray[index];
while(el != NULL)
{
if (el->key == key)
{
value = el->value;
return el->value;
}
el = el->next;
}
return "name not found";
}
int main()
{
setlocale(LC_ALL, "Russian");
MyHashTable<string> table(10);
string str="9047925278";
table.set(str,"Паша");
table.set("9092848829","Маша Воронина");
table.set("9040449664","Маша Еремина");
table.set("9601618844","Инна Васильева");
table.printLog();
string str2;
string str3;
table.get(str,str2);
table.get("9092848829",str3);
table.get("9601618844",str3);
cout << "Имя номера 909284 88 29 - "<< table.getname("9092848829") << endl;
cout << "Имя номера 904044 96 64 - "<< table.getname("9040449664") << endl;
cout << "Имя номера 960161 88 44 - "<< table.getname("9601618844") << endl;
cout << "Имя номера 904792 52 78 - "<< table.getname("9047925278") << endl;
table.clear();
return 0;
}