
Use this hashmap to solve
#include<iostream>
#include<list>
using namespace std;
struct Pair {
char key;
bool value;
};
class Hashmap {
private:
static const int buckets = 8;
list<Pair> table[buckets];
public:
bool isEmpty();
int hashFunc(int key);
void insert(const Pair& item);
void remove(int key);
Pair search(int key); // returns pair
void print();
};
bool Hashmap::isEmpty() {
for (int i = 0; i < buckets; i++) {
if (table[i].size() > 0)
return false;
}
return true;
}
int Hashmap::hashFunc(int key) {
return key % buckets;
}
void Hashmap::insert(const Pair& item) {
int hash = hashFunc(item.key);
auto& chain = table[hash]; // pointer to a linked list
// pointer to a head
bool exist = false;
for (auto it = chain.begin(); it != chain.end(); it++) {
if (it->key == item.key) {
exist = true;
it->value = item.value;
cout << "Key exist. Value was replaced" << endl;
break;
}
}
if (!exist) {
chain.emplace_back(item);
}
}
void Hashmap::remove(int key) {
int hash = hashFunc(key);
auto& chain = table[hash]; // pointer to a linked list
auto it = chain.begin(); // pointer to a data of the first node
bool exist = false;
for (; it != chain.end(); it++) {
if (it->key == key) {
exist = true;
it = chain.erase(it); // return a pointer to next value
cout << "Element was removed" << endl;
break;
}
}
if (!exist) {
cout << "Element was not found" << endl;
}
}
void Hashmap::print() {
for (int i = 0; i < buckets; i++) {
if (table[i].size() == 0) continue;
auto it = table[i].begin();
for (; it != table[i].end(); it++)
cout << "Key: " << it->key <<
" Value: " << it->value << endl;
}
}
int main() {
Hashmap map;
if (map.isEmpty()) {
cout << "Empty" << endl;
}
else {
cout << "Problem" << endl;
}
map.insert({ 'a', 0 });
map.insert({ 'd', 0 });
map.insert({ 'f', 0 });
map.insert({ 'e', 0 });
map.insert({ 'a', 1 });
map.insert({ 't', 0 });
map.insert({ 'd', 1 });
map.insert({ 'b', 0 });
map.print();
map.remove('a');
map.remove('d');
map.remove('f');
map.remove('f');
map.remove('e');
map.remove('b');
map.remove('t');
map.remove('d');
if (map.isEmpty()) {
cout << "Good job!" << endl;
}
else {
cout << "Problem!!!" << endl;
}
//given a string return first recurring character using implemented Hashtable
return 0;
}


Trending nowThis is a popular solution!
Step by stepSolved in 4 steps with 2 images

- struct insert_at_back_of_sll { // Function takes a constant Book as a parameter, inserts that book at the // back of a singly linked list, and returns nothing. void operator()(const Book& book) { /// TO-DO (3) /// // Write the lines of code to insert "book" at the back of "my_sll". Since // the SLL has no size() function and no tail pointer, you must walk the // list looking for the last node. // // HINT: Do not attempt to insert after "my_sll.end()". // ///// END-T0-DO (3) ||||// } std::forward_list& my_sll; };arrow_forward#include <iostream> usingnamespace std; class Queue { int size; int* queue; public: Queue(){ size = 0; queue = new int[100]; } void add(int data){ queue[size]= data; size++; } void remove(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } else{ for(int i =0; i < size -1; i++){ queue[i]= queue[i +1]; } size--; } } void print(){ if(size ==0){ cout <<"Queue is empty"<<endl; return; } for(int i =0; i < size; i++){ cout<<queue[i]<<" <- "; } cout << endl; } //your code goes here }; int main(){ Queue q1; q1.add(42); q1.add(2); q1.add(8); q1.add(1); Queue q2; q2.add(3); q2.add(66); q2.add(128); q2.add(5); Queue q3 = q1+q2; q3.print();…arrow_forwardC++ Hello im trying to make a list for my code but i cant use the <list> library, how would i make a list for my linked list array based hashtable code wordlist: incase you want to test the entire code a aaa hello goodbye #pragma once //hashtable.h #include <iostream> #include <string> #include <list> #include <cstdlib> #include "word.h" using namespace std; class HashTable { int BUCKET; list<Word>* table; public: HashTable(int W); //to check if the table is empty bool isEmpty() const; //to insert a key into the hashtable void insertItem(string& x); //to delete a key from the hashtable void deleteItem(int key); int hashFunction(Word x); //to look for the key in the table bool searchTable(string& key); void displayHash(); }; //hashtable.cpp #include "hashtable.h" #include <iostream> HashTable::HashTable(int W) { table = new list < Word >[W]; BUCKET = W; return; } //to check if the table is empty bool…arrow_forward
- Data structure Javaarrow_forwardGiven the following definition for a LinkedList: // LinkedList.h class LinkedList { public: LinkedList(); // TODO: Implement me void printEveryOther() const; private: struct Node { int data; Node* next; }; Node * head; }; // LinkedList.cpp #include "LinkedList.h" LinkedList::LinkedList() { head = nullptr; } Implement the function printEveryOther, which prints every other data value (i.e. those at the odd indices assuming 0-based indexing).arrow_forwardWhat feature of Outlook could you use in the medical office frequently?arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





