我正在實現一個程序來搜索文件中的字符串。我做的是我要求用戶輸入文件的名稱和文件的內容,然後用空格標記內容作爲分隔符並創建它的哈希表。例如:從HashTable刪除基於C++值不使用STL
filename-abc.txt content- i am bad than u filename-xyz.txt content-u r awesome
。
我的哈希表如下所示:
i->abc.txt
m->abc.txt
bad->abc.txt
than->abc.txt
u->abc.txt->xyz.txt
r->xyz.txt
awesome->xyz.txt
我不得不做了很多操作,但一個這樣的操作是delete the filename
這意味着,如果用戶要求刪除xyz.txt將該,HashMap的應該像
i->abc.txt
m->abc.txt
bad->abc.txt
than->abc.txt
u->abc.txt
所有這一切都在內存中發生的事情,我已經創建了自己的hashnode
和hashmap
,不使用C++ STL
。
我HashNode看起來像這樣
class HashNode
{
public:
int key;
string value;
HashNode* next;
HashNode(int key, string value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
的Hashmap是這樣
class HashMap
{
private:
HashNode** htable;
public:
HashMap()
{
htable = new HashNode*[TABLE_SIZE];
for (int i = 0; i < TABLE_SIZE; i++)
htable[i] = NULL;
}
我將如何執行刪除文件操作。
根據你的代碼,刪除'filename'只是一個從單個鏈表中刪除一個節點的任務。但是你需要解析存在於你的散列表的每個索引上的鏈表。您可以創建第二個hastable來代替解析完整的散列表,告訴您哪個文件存在於第一個散列表的哪個索引處。 – sameerkn