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;
}
//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
Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...
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...
Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion. #include <iostream> using namespace std; //----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; }; //----------- Define Linked List...
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 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...
In C++ I am trying to implement a log base 2 hash table utilizing the shift function. So, the code determines the position of where the value will be stored using log2. But am having issues using the shift part as everything just wants to pile on to zero. //ChainingHash - log2 #include<iostream> #include<vector> #include<iterator> #include<string> #include<cmath> using namespace std; #define BUCKET 10 //no. of buckets class Hash { vector<int>*table; //ptr containing buckets public: Hash();...
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 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 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 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 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...