close
看到最近有些人連這文章,
為了避免誤導加點註解了好,
這其實不太算是hashtable.
只是自己寫了一個有點類似的data struct
#include <iostream>
using namespace std;
class HashTable
{
public:
HashTable();
~HashTable();
HashTable(const char & temp);
const HashTable * Search(const char * temp);
const HashTable * Search(const char & temp);
bool Insert(const char *temp);
private:
HashTable * AnotherCh;
HashTable * SubCh;
char node;
};
int main()
{
HashTable a('b');
a.Insert("abcede");
a.Insert("abcg");
a.Insert("abcgf");
a.Search("abce");
return 0;
}
HashTable::HashTable()
{
node = NULL;
AnotherCh = NULL;
SubCh = NULL;
}
HashTable::HashTable(const char & temp)
{
node = temp;
AnotherCh = NULL;
SubCh = NULL;
}
HashTable::~HashTable()
{
delete AnotherCh;
delete SubCh;
}
const HashTable * HashTable::Search(const char *temp)
{
if(this->node == NULL)
return NULL;
else if(this->node == *temp)
{
if(*(temp+1) == '\0')
return this;
else
{
if(SubCh != NULL)
return SubCh->Search((temp+1));
else
return NULL;
}
}
else
{
if(AnotherCh != NULL)
return AnotherCh->Search((temp));
else
return NULL;
}
}
const HashTable * HashTable::Search(const char & temp)
{
if(this->node == NULL)
return false;
else if(this->node == temp)
return this;
else
{
if(AnotherCh != NULL)
return AnotherCh->Search((temp));
else
return false;
}
}
bool HashTable::Insert(const char * temp)
{
if(*temp!=NULL)
{
if(this->node == NULL)
{
this->node = *temp;
if(*(temp+1)!=NULL)
{
if(this->SubCh == NULL)
{
this->SubCh = new HashTable;
return this->SubCh->Insert(temp + 1);
}
else
{
return this->SubCh->Insert(temp + 1);
}
}
}
else
{
if(*temp != this->node)
{
if(this->AnotherCh != NULL)
return this->AnotherCh->Insert(temp);
else
{
this->AnotherCh = new HashTable;
return this->AnotherCh->Insert(temp);
}
}
else
{
if(this->SubCh == NULL)
{
this->SubCh = new HashTable;
return this->SubCh->Insert(temp + 1);
}
else
{
return this->SubCh->Insert(temp + 1);
}
}
}
}
return true;
}
全站熱搜
留言列表