Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*** THIS CODE IS FOR INT ONLY, I NEED TO IMPLEMENT FOR STRINGS #include #include #include #include using namespace std; const int TABLE_SIZE = 128;

image text in transcribed

*** THIS CODE IS FOR INT ONLY, I NEED TO IMPLEMENT FOR STRINGS

#include

#include

#include

#include

using namespace std;

const int TABLE_SIZE = 128;

/*

* HashEntry Class Declaration

*/

class HashEntry

{

public:

int key;

int value;

HashEntry(int key, int value)

{

this->key = key;

this->value = value;

}

};

/*

* HashMap Class Declaration

*/

class HashMap

{

private:

HashEntry **table;

public:

HashMap()

{

table = new HashEntry * [TABLE_SIZE];

for (int i = 0; i

{

table[i] = NULL;

}

}

/*

* Hash Function

*/

int HashFunc(int key)

{

return key % TABLE_SIZE;

}

/*

* Insert Element at a key

*/

void Insert(int key, int value)

{

int hash = HashFunc(key);

while (table[hash] != NULL && table[hash]->key != key)

{

hash = HashFunc(hash + 1);

}

if (table[hash] != NULL)

delete table[hash];

table[hash] = new HashEntry(key, value);

}

/*

* Search Element at a key

*/

int Search(int key)

{

int hash = HashFunc(key);

while (table[hash] != NULL && table[hash]->key != key)

{

hash = HashFunc(hash + 1);

}

if (table[hash] == NULL)

return -1;

else

return table[hash]->value;

}

/*

* Remove Element at a key

*/

void Remove(int key)

{

int hash = HashFunc(key);

while (table[hash] != NULL)

{

if (table[hash]->key == key)

break;

hash = HashFunc(hash + 1);

}

if (table[hash] == NULL)

{

cout

return;

}

else

{

delete table[hash];

}

cout

}

~HashMap()

{

for (int i = 0; i

{

if (table[i] != NULL)

delete table[i];

delete[] table;

}

}

};

int main()

{

HashMap hash;

int key, value;

int choice;

while (true)

{

cout

cout

cout

cout

cout

cout

cout

cout

cin>>choice;

switch(choice)

{

case 1:

cout

cin>>value;

cout

cin>>key;

hash.Insert(key, value);

break;

case 2:

cout

cin>>key;

if (hash.Search(key) == -1)

{

cout

continue;

}

else

{

cout

cout

}

break;

case 3:

cout

cin>>key;

hash.Remove(key);

break;

case 4:

exit(1);

default:

cout

}

}

return 0;

}

Task 1: HardCode a C Hash Table that Will Accept a Key as String and Map to String Value. First Name Jane" "John" "Susan" Last Name "Smith" "Do" "Collins" "Rodgers" Jones" "Wright" "Bader" "Eric" "Mike" Create Methods to Insert: insert(key, value) insert("First", "Last") Findvalue("First")-> Method will return value Delete("First") - Method will delete key and value

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

OCA Oracle Database SQL Exam Guide Exam 1Z0-071

Authors: Steve O'Hearn

1st Edition

1259585492, 978-1259585494

More Books

Students also viewed these Databases questions

Question

Explain the causes of indiscipline.

Answered: 1 week ago

Question

13. You always should try to make a good first impression.

Answered: 1 week ago