Only write the code for void HashTable::createNewHashTable() while using int has
ID: 3828533 • Letter: O
Question
Only write the code for void HashTable::createNewHashTable() while using int hashSum2(std::string x, int s). No other part of the code has to be written.
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. Note: In the background, we handle building/inserting into the hash table by the hash function from your book. We also write the hashsum2 and you only will need to call it. You will need to use the newHashTable variable. void HashTable::createNewHashTable0; You need to implement this function int hashSum2(std: string x, int s); already defined. You will need to call this function in createNewHashTable0Explanation / Answer
Given below is the code for Movie struct, HashTable class and Main driver program. For completeness sake all code is provided. Compilation and execution commands along with execution output is mentioned below.
The driver program simply instantiates an object of HashTable class and calls methods for creation of new hash table with expected data and print of populated new hash table.
createNewHashTable public method of HashTable class makes use of a private method insertNewHashTable to insert a movie into the new hash table.
File: Movie.h
struct Movie {
std::string title;
Movie *next;
Movie() {}
Movie(std::string in_title) {
title = in_title;
next = NULL;
}
};
File: HashTable.h
#include <iostream>
#include "Movie.h"
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];
};
File: HashTable.cpp
#include <iostream>
#include "HashTable.h"
HashTable::HashTable()
{
tableSize = 10;
for (int i = 0; i < 10; i++)
{
newHashTable[i] = NULL;
}
}
HashTable::~HashTable()
{
Movie *movie;
Movie *nextMovie;
for (int i = 0; i < tableSize; i++)
{
movie = newHashTable[i];
newHashTable[i] = NULL;
while (movie != NULL)
{
nextMovie = movie->next;
delete movie;
movie = nextMovie;
}
}
}
void HashTable::createNewHashTable()
{
int hash;
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 i = 0; i < tableSize; i++)
{
std::cout << "Index " << i << ":" << std::endl;
Movie *movie = newHashTable[i];
while (movie != NULL)
{
std::cout << movie->title;
movie = movie->next;
if (movie != NULL)
{
std::cout << " => ";
} else {
std::cout << std::endl;
}
}
std::cout << std::endl;
}
}
void HashTable::insertNewHashTable(std::string title) {
int hashIndex = hashSum2(title, tableSize);
if (newHashTable[hashIndex] == NULL)
{
newHashTable[hashIndex] = new Movie(title);
}
else
{
Movie *movie = new Movie(title);
movie->next = newHashTable[hashIndex];
newHashTable[hashIndex] = movie;
}
}
// 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;
}
File: Main.cpp (driver program)
#include <iostream>
#include "HashTable.h"
int main() {
HashTable hashTable;
hashTable.createNewHashTable();
hashTable.printNewHashTable();
return 0;
}
Compilation:
$ g++ HashTable.cpp Main.cpp
Execution Output:
$ ./a.out
-------------- NEW HASH TABLE --------------
Index 0:
Index 1:
The Godfather
Index 2:
Pulp Fiction => The Lord of the Rings: The Fellowship of the Ring
Index 3:
The Matrix => Fight Club
Index 4:
Star Wars: Episode V - The Empire Strikes Back
Index 5:
Schindler's List
Index 6:
The Good the Bad and the Ugly => The Lord of the Rings: The Return of the King
Index 7:
Shawshank Redemption
Index 8:
12 Angry Men => The Dark Knight
Index 9:
Inception => The Godfather: Part II
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.