Question

Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you.

// hash.CPP program to implement hashing with chaining
#include<iostream>
#include "hash.hpp"


using namespace std;

node* HashTable::createNode(int key, node* next)
{
node* nw = new node;
nw->key = key;
nw->next = next;
return nw;
}

HashTable::HashTable(int bsize)
{
this->tableSize= bsize;
table = new node*[tableSize];
for(int i=0;i<bsize;i++)
table[i] = nullptr;
}

//function to calculate hash function
unsigned int HashTable::hashFunction(int key)
{
return (key % tableSize);
}

// TODO Complete this function
//function to search
node* HashTable::searchItem(int key)
{
//Compute the index by using the hash function
int index = hashFunction(key);

//TODO: Search the list at that specific index and return the node if found
}

//TODO Complete this function
//function to insert
bool HashTable::insertItem(int key)
{
if(!searchItem(key))
{
// TODO :
// Use the hash function on the key to get the index/slot,
// create a new node with the key and insert it in this slot's list

}
else{
cout<<"duplicate entry: "<<key<<endl;
return false;
}

}

//TODO Complete this function
// function to display hash table
void HashTable::printTable()
{
for (int i = 0; i < tableSize; i++) {
cout << i <<"|| ";

//TODO
}

}

///////////////////////hash.hpp//////////////////////

#ifndef HASH_HPP
#define HASH_HPP

#include <string>


using namespace std;

struct node
{
int key;
struct node* next;
};

class HashTable
{
int tableSize; // No. of buckets (linked lists)

// Pointer to an array containing buckets
node* *table;

node* createNode(int key, node* next);
public:
HashTable(int bsize); // Constructor

// inserts a key into hash table
bool insertItem(int key);

// hash function to map values to key
unsigned int hashFunction(int key);

void printTable();

node* searchItem(int key);
};

#endif

////////////////////driver.cpp

// Driver program
#include<iostream>
#include "hash.hpp"
using namespace std;

// GOLD TODO Complete this function
void printPairs(int* arr, int size, int sum, HashTable ht){

bool pairFound = false;

//TODO :
// iterate over each element in the array,
// search for sum-arr[i] in the hashtable, if you find it, print out the pairs giving this sum
// and you are done!

if(pairFound==false){
cout<<"pair not found";
}

}

int main()
{
int a[] = {15, 11, 27, 8, 12, 7, 18};
int n = 7;

// 7 is count of buckets in hash table
HashTable ht(7);

// insert the keys into the hash table.
// SILVER TODO : Complete insertItem() function
for (int i = 0; i < n; i++) {
ht.insertItem(a[i]);
}

cout<< "Contents of the hash table are"<<endl;
// SILVER TODO : Complete printTable() function
ht.printTable();

cout<<endl;

int searchFor = 8;
// SILVER TODO : Complete searchItem() function
if(ht.searchItem(searchFor))
{
cout<<searchFor<<" found in the hash table"<<endl;
}
else{
cout<<searchFor<<" not found in the hash table"<<endl;
}
cout<<"------------------------------------"<<endl;

int sum = 19;

// GOLD TODO Complete printPairs() function
printPairs(a,n,sum,ht);

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp.The hash function has already been implemented for you.
////////////////////driver.cpp
// Driver program
#include<iostream>
#include <string>
using namespace std;

///////////////////////hash.hpp//////////////////////
struct node
{
   int key;
   struct node* next;
};
class HashTable
{
   int tableSize; // No. of buckets (linked lists)

   // Pointer to an array containing buckets
   node* *table;

   node* createNode(int key, node* next);
public:
   HashTable(int bsize); // Constructor

   // inserts a key into hash table
   bool insertItem(int key);

   // hash function to map values to key
   unsigned int hashFunction(int key);

   void printTable();

   node* searchItem(int key);
};

node* HashTable::createNode(int key, node* next)
{
   node* nw = new node;
   nw->key = key;
   nw->next = next;
   return nw;
}
HashTable::HashTable(int bsize)
{
   this->tableSize = bsize;
   table = new node*[tableSize];
   for (int i = 0; i < bsize; i++)
       table[i] = nullptr;
}
//function to calculate hash function
unsigned int HashTable::hashFunction(int key)
{
   return (key % tableSize);
}
// TODO Complete this function
//function to search
node* HashTable::searchItem(int key)
{
   //Compute the index by using the hash function
   int index = hashFunction(key);
   node* temp = table[index];
   while (temp != nullptr)
   {
       if (temp->key == key)
           return temp;
       temp = temp->next;
   }
   return nullptr;
   //TODO: Search the list at that specific index and return the node if found
}
//TODO Complete this function
//function to insert
bool HashTable::insertItem(int key)
{
   if (!searchItem(key))
   {
       // TODO :
       // Use the hash function on the key to get the index/slot,
       // create a new node with the key and insert it in this slot's list
       int index = hashFunction(key);
       table[index] = createNode(key, table[index]);
       return true;
   }
   else {
       cout << "duplicate entry: " << key << endl;
       return false;
   }
}
//TODO Complete this function
// function to display hash table
void HashTable::printTable()
{
   for (int i = 0; i < tableSize; i++) {
       cout << i << "||";
       node* temp = table[i];
       while (temp != nullptr)
       {
           cout << temp->key << " ";
           temp = temp->next;
       }
       cout << endl;
       //TODO
   }

}
// GOLD TODO Complete this function
void printPairs(int* arr, int size, int sum, HashTable ht) {

   bool pairFound = false;

   //TODO :
   // iterate over each element in the array,
   // search for sum-arr[i] in the hashtable, if you find it, print out the pairs giving this sum
   // and you are done!

  
   for (int i = 0; i < size; i++)
   {
       int toFind = sum - arr[i];
       node* val = ht.searchItem(toFind);
       if (val)
       {      
           cout << arr[i] << " + " << val->key << " = " << sum << endl;
           pairFound = true;
           break;
       }
   }

   if (pairFound == false) {
       cout << "pair not found";
   }
   else cout << "pair found";
}

int main()
{
   int a[] = { 15, 11, 27, 8, 12, 7, 18 };
   int n = 7;

   // 7 is count of buckets in hash table
   HashTable ht(7);

   // insert the keys into the hash table.
   // SILVER TODO : Complete insertItem() function
   for (int i = 0; i < n; i++) {
       ht.insertItem(a[i]);
   }

   cout << "Contents of the hash table are" << endl;
   // SILVER TODO : Complete printTable() function
   ht.printTable();

   cout << endl;

   int searchFor = 8;
   // SILVER TODO : Complete searchItem() function
   if (ht.searchItem(searchFor))
   {
       cout << searchFor << " found in the hash table" << endl;
   }
   else {
       cout << searchFor << " not found in the hash table" << endl;
   }
   cout << "------------------------------------" << endl;

   int sum = 19;

   // GOLD TODO Complete printPairs() function
   printPairs(a, n, sum, ht);

   return 0;
}

COMMENT DOWN FOR ANY QUERY

PLEASE GIVE A THUMBS UP

Add a comment
Know the answer?
Add Answer to:
Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly...

    Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • How do I do this? -> string print() const; Function to output the data, return the...

    How do I do this? -> string print() const; Function to output the data, return the output in the form of string, with all elements in the hash table included and each seperated by a space this is my code: template <class elemType> string hashT<elemType>::print() const { for (int i = 0; i < HTSize; i++){        if (indexStatusList[i] == 1){        cout <<HTable[i]<< " " << endl;        }   }    } **********Rest of the code...

  • C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can...

    C programming Problem 3 [Set via Hashing] As mentioned in the lecture, a hash table can be used to implement a Set ADT. Let's try to use the template below to implement a Set with double hashing. Here we assume the Set contains names with 3 characters long. Since it is a Set, we do not need to have an explicit value for each key. We will use a token value of 1 if a name belongs to the Set....

  • In this assignment, you will use a Hashtable to determine the common elements in all the...

    In this assignment, you will use a Hashtable to determine the common elements in all the lists. If an element appears more than once in one or more lists, the algorithm should capture the instances the element is present in all the lists. You are given a code that implements a Hashtable as an array of linked lists. You are also given a main function that will create an array of Lists using the input variable values that you enter....

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • This is the incomplete example from class last week, where we started implementing a hash table...

    This is the incomplete example from class last week, where we started implementing a hash table as a vector of queues. In order for this example to work, the Queue struct needs 3 more functions to be implemented. A find function, which tells us it a given value appears in the queue, without being destructive. A function called print, which prints out the contents of the queue, again without being destructive. Finally, there is a need for a destructor. To...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • //This program is your final exam. //Please fill in the functions at the bottom of the...

    //This program is your final exam. //Please fill in the functions at the bottom of the file. (evenCount and insertItem) //DO NOT CHANGE ANYTHING ELSE. //main has all the code needed to test your functions. Once your functions are written, please build and make sure it works fine //Note that in this case, the list is not sorted and does not need to be. Your goal is to insert the number in the given position. #include <iostream> #include <fstream> using...

  • Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into...

    Hash Tables. (Hint: Diagrams might be helpful for parts a) and b). ) When inserting into hash table we insert at an index calculated by the key modulo the array size, what would happen if we instead did key mod (array_size*2), or key mod (array_size/2)? (Describe both cases). Theory answer Here Change your hashtable from the labs/assignments to be an array of linkedlists – so now insertion is done into a linkedlist at that index. Implement insertion and search. This...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT