哈希表C ++字符串

时间:2019-03-07 10:29:31

标签: c++ string hashtable

尝试在C ++中实现哈希表,该表必须接收字符串数据,并且必须至少包含10个项目。在下面实现了此功能,但未进行编译,并且已以某种方式破坏了它:(,对其他最佳实现方法或对此方法的解决方案持开放态度

谢谢 请有人成为传奇。 :)

#include <iostream>
#include <cstdlib>
#include <string>


using namespace std;


class hash{
   private:
      static const int tableSize = 10;

      struct item
      {
        string d;
        item* next;
      };

      item* HashTable[tableSize];

   public:
      hash();//the constructor
      int Hash(string key);
      void AddItem(string d);//will add new item
      int NumberOfItemsInBucket(int bucket);
      void PrintTable();
      void PrintItemsInBucket(int bucket);
};


hash::hash()
{
   for(int i = 0;i < tableSize;i++)
   {
      HashTable[i] = new item;
      HashTable[i]->d = "";
      HashTable[i]->next = NULL;
   }
};


void hash::AddItem(string d)
{
   int bucket = Hash(d);

   if(HashTable[bucket]->d == "")
   {
      HashTable[bucket]->d = d;
   }
   else
   {
      item* Ptr = HashTable[bucket];
      item* n = new item;
      n->d = d;
      n->next = NULL;
      while(Ptr->next != NULL)
      {
        Ptr = Ptr->next;    
      }
       Ptr->next;
   }
}


int hash::NumberOfItemsInBucket(int bucket)
{
   int slot = 0;
   if(HashTable[bucket]->d == "")
   {
      return slot;    
   }
   else
   {
      slot++;
      item* Ptr = HashTable[bucket];
      while(Ptr->next != NULL)
      {
        slot++;
        Ptr = Ptr->next;
      }
   }
   return slot;
}


void hash::PrintTable()
{
   int number;
   for(int i = 0;i < tableSize;i++)
   {
      number = NumberOfItemsInBucket(i);
      cout << "--------------------\n";
      cout << "bucket = " << i << endl;
      cout << "Data: " << HashTable[i]->d << endl;
      cout << "No. of items = " << number << endl;
      cout << "--------------------\n";
   }
}


void hash::PrintItemsInBucket(int bucket){


   item* Ptr = HashTable[bucket];

   if(Ptr->d == ""){
   cout << "bucket " << bucket << " is empty!\n";
   }else{
      cout << "Bucket " << bucket << " contains this: " << endl;
      while(Ptr != NULL){
        cout << "--------------------\n";
        cout << Ptr->d << endl;
        cout << "--------------------\n";
        Ptr = Ptr->next;
      }
   }

}


int hash::Hash(string key){
   int hash = 0;
   int index;

   for(int i = 0;i < key.length();i++)
   {
      hash = hash + (int)key[i];
      //cout << "Hash = " << hash << endl;    //displays the hash function result
   }
   index = hash % tableSize;
   return index;
}


int main (){
   hash newHash;


    newHash.AddItem("restaurant");
    newHash.AddItem("innovation");
     newHash.AddItem("vegetarian");
    newHash.AddItem("opposition");
    newHash.AddItem("attractive");
    newHash.AddItem("incredible");
    newHash.AddItem("assessment");
    newHash.AddItem("illustrate");
     newHash.AddItem("presidency");
    newHash.AddItem("background");

   newHash.PrintTable();
   //newHash.PrintItemsInBucket();
   return 0;
}

编译错误: 注意:类哈希 错误:范围中未声明“ newHash” 错误:对“哈希”的引用不明确

1 个答案:

答案 0 :(得分:0)

只需删除using manespace std;并显式添加std ::到endl,cout和string。