Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a C++ function to build a new hash table from an existing hash table u

ID: 3825902 • Letter: I

Question

Implement a C++ function to build a new hash table from an existing hash table using a new hash function called hashSum2. In the new hash table, insert the movies at the front of the linked list when the movies hash to the same index. void HashTable::createNewHashTable();//You need to implement this function int hashSum2(std::string x, int s); createNewHashTable//already defined. You will need to call this function in struct Movie{std::string title; Movie *next; Movie(){}; Movie(std::string in_title){title = in_title; next = NULL;}}; class HashTable {public: HashTable(); ~HashTable(); void createNewHashTable(); private: int hashSum(std::string x, int tablesize); int hashSum2(std::string x, int s); int tableSize; Movie * hashTable[10]; Movie * newHashTable[10];};//Returns hashSum2 of input string x by summing every other letter in string int HashTable::hashSum2(string inputString, int hashLen) {int sum = 0; for (int i = 0; i Fight Club => The Matrix Index 1: The Lord of the Rings: The Fellowship of the Ring = > The Lord of the Rings: The Return of the Index 2: Schindler's List = > The Dark Knight = > The Godfather: Part II Index 3: The Good the Bad and the Ugly Index 4: 12 Angry Men Index 5: Pulp Fiction = > Shawshank Redemption Pulp Fiction => Shawshank Redemption Index 6: Index 7: The Godfather = > Inception Index 8: Index 9:

Explanation / Answer

Solution:

#include <iostream>
#include <string>
using namespace std;
struct Movie
{
   std::string title;
   Movie *next;
   Movie() {}
   Movie(std::string in_title)
   {
   title = in_title;
   next = NULL;
   }
};

class HashTable {
   public:
   HashTable();
   ~HashTable();

   void createNewHashTable();
   void printNewHashTable();

   private:
   void insertNewHashTable(std::string x);

   int hashSum(std::string x, int tablesize);
   int hashSum2(std::string x, int s);

   int tableSize;
   Movie *hashTable[10];
   Movie *newHashTable[10];
};

// Returns hashSum2 of input string x by summing every other letter in string
int HashTable::hashSum2(std::string inputString, int hashLen)
{
int sum = 0;
for (int i = 0; i < inputString.length(); i++) {
if (i % 2 == 0)
sum = sum + inputString[i];
}
sum = sum % hashLen;
return sum;
}

HashTable::HashTable()
{
   tableSize = 10;
   for (int li = 0; li < 10; li++)
   {
   newHashTable[li] = NULL;
   }
}

HashTable::~HashTable()
{
   Movie *HTmovie;
   Movie *HTnextMovie;

   for (int li = 0; li < tableSize; li++)
   {
   HTmovie = newHashTable[li];
   newHashTable[li] = NULL;

   while (HTmovie != NULL)
   {
   HTnextMovie = HTmovie->next;
   delete HTmovie;
   HTmovie = HTnextMovie;
   }
   }
}

void HashTable::createNewHashTable()
{
   insertNewHashTable("The Godfather");
   insertNewHashTable("The Lord of the Rings: The Fellowship of the Ring");
   insertNewHashTable("Pulp Fiction");
   insertNewHashTable("Fight Club");
   insertNewHashTable("The Matrix");
   insertNewHashTable("Star Wars: Episode V - The Empire Strikes Back");
   insertNewHashTable("Schindler's List");
   insertNewHashTable("The Lord of the Rings: The Return of the King");
   insertNewHashTable("The Good the Bad and the Ugly");
   insertNewHashTable("Shawshank Redemption");
   insertNewHashTable("The Dark Knight");
   insertNewHashTable("12 Angry Men");
   insertNewHashTable("The Godfather: Part II");
   insertNewHashTable("Inception");
  
}

void HashTable::printNewHashTable()
{
   std::cout << "-------------- NEW HASH TABLE --------------" << std::endl;

   for (int li = 0; li < tableSize; li++)
   {
       std::cout << "Index " << li << ":" << std::endl;

       Movie *HTmovie = newHashTable[li];
       while (HTmovie != NULL)
       {
           cout<<HTmovie->title;

           HTmovie = HTmovie->next;
           if (HTmovie != NULL)
           {
               cout << " => ";
           } else
           {
               cout << std::endl;
           }
       }
       cout << std::endl;
   }
}

void HashTable::insertNewHashTable(std::string title)
{
   int HThashIndex = hashSum2(title, tableSize);

   if (newHashTable[HThashIndex] == NULL)
   {
       newHashTable[HThashIndex] = new Movie(title);
   }
   else
   {
       Movie *HTmovie = new Movie(title);
       HTmovie->next = newHashTable[HThashIndex];
       newHashTable[HThashIndex] = HTmovie;
   }
}

int main()
{
   HashTable HThashTable;
   HThashTable.createNewHashTable();
   HThashTable.printNewHashTable();
   system("pause");
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote