哈希表C 字符串

hash table c++ strings

本文关键字:字符串 哈希表      更新时间:2023-10-16

试图在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"错误:引用"哈希"是模棱两可的

只需删除使用Manespace STD即可删除;并显式添加添加std ::到endl,cout和string。