Search code examples
c++stringhashtable

hash table c++ strings


Trying to Implement a hash table in C++, Where the table has to take in string data and must hold at least 10 items. Implemented this below but doesn't compile and have broke it somehow :(, open to other ideas on how is best to implement or a fix for this one

Thanks Someone be a legend please. :)

#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;
}

Compile errors: note: class hash error: 'newHash' not declared in the scope error: reference to 'hash' is ambiguous


Solution

  • Just remove the using manespace std; and add explicitly add std:: to endl, cout, and string.