1. Implement search function for the Hashmap class provided below. If element is not found return an empty Pair object and display "Item is not found".

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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;
}

1. Implement search function for the Hashmap class provided below. If element is not found return an empty Pair
object and display "Item is not found".
Transcribed Image Text:1. Implement search function for the Hashmap class provided below. If element is not found return an empty Pair object and display "Item is not found".
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY