*** THIS CODE IS FOR INT ONLY, I NEED TO IMPLEMENT FOR STRINGS #include<iostream
ID: 3710661 • Letter: #
Question
*** THIS CODE IS FOR INT ONLY, I NEED TO IMPLEMENT FOR STRINGS
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
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_SIZE; 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<<"No Element found at key "<<key<<endl;
return;
}
else
{
delete table[hash];
}
cout<<"Element Deleted"<<endl;
}
~HashMap()
{
for (int i = 0; i < TABLE_SIZE; i++)
{
if (table[i] != NULL)
delete table[i];
delete[] table;
}
}
};
int main()
{
HashMap hash;
int key, value;
int choice;
while (true)
{
cout<<" ----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<" ----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter element to be inserted: ";
cin>>value;
cout<<"Enter key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter key of the element to be searched: ";
cin>>key;
if (hash.Search(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
exit(1);
default:
cout<<" Enter correct option ";
}
}
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 valueExplanation / Answer
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
const int TABLE_SIZE = 128;
/*
* HashEntry Class Declaration
*/
class HashEntry
{
public:
// To store key value as first name
string keyFirstName;
// To store the value as last name
string valueLastName;
// Parameterized constructor to assign parameter values to data member
HashEntry(string keyFirstName, string valueLastName)
{
this->keyFirstName = keyFirstName;
this->valueLastName = valueLastName;
}// End of constructor
};// End of class
/*
* HashMap Class Declaration
*/
class HashMap
{
private:
// Creates a pointer to pointer type of HashEntry class
HashEntry **table;
public:
// Default constructor
HashMap()
{
// Dynamically allocates memory to table of size TABLE_SIZE which is 128
table = new HashEntry * [TABLE_SIZE];
// Loops till TABLE_SIZE
for (int i = 0; i < TABLE_SIZE; i++)
// Assigns NULL to each element
table[i] = NULL;
}// End of function
/*
* Hash Function
*/
int HashFunc(int key)
{
// Returns remainder
return key % TABLE_SIZE;
}// End of function
/*
* Insert Element at a key
*/
void Insert(string key, string value)
{
// Calls the function to get the position based on the first character of key
int hash = HashFunc((int)key.at(0));
// Loops till table's hash index position is not NULL and table's keyFirstName is not equals to parameter key
while (table[hash] != NULL && table[hash]->keyFirstName != key)
// Calls the function with next of hash value
hash = HashFunc(hash + 1);
// Checks if table's hash index position is not null
if (table[hash] != NULL)
// Delete the table's hash index position data
delete table[hash];
// Creates a new object of class HashEntry using parameterized constructor and assigns it to table's hash index position
table[hash] = new HashEntry(key, value);
}// End of function
/*
* Search Element at a key
*/
string Search(string key)
{
// Calls the function to get the position based on the first character of key
int hash = HashFunc((int)key.at(0));
// Loops till table's hash index position is not NULL and table's keyFirstName is not equals to parameter key
while (table[hash] != NULL && table[hash]->keyFirstName != key)
// Calls the function with next of hash value
hash = HashFunc(hash + 1);
// Checks if table's hash index position is null then returns null
if (table[hash] == NULL)
return " ";
// Otherwise
else
// returns the table's hash index position data
return table[hash]->valueLastName;
}// End of function
/*
* Remove Element at a key
*/
void Remove(string key)
{
// Calls the function to get the position based on the first character of key
int hash = HashFunc((int)key.at(0));
// Loops till table's hash index position is not NULL
while (table[hash] != NULL)
{
// Checks if table's keyFirstName is equals to parameter key then come out of the loop
if (table[hash]->keyFirstName == key)
break;
// Otherwise, Calls the function with next of hash value
hash = HashFunc(hash + 1);
}// End of while loop
// Checks if table's hash index position is null then key not found
if (table[hash] == NULL)
{
cout<<"No Element found at key "<<key<<endl;
return;
}// End of if condition
// Otherwise, key found
else
{
// Delete the key located at table's hash index position
delete table[hash];
// Assign null to the deleted position
table[hash] = NULL;
}
cout<<"Element Deleted"<<endl;
}// End of function
// Destructor definition
~HashMap()
{
// Loops till TABLE_SIZE
for (int i = 0; i < TABLE_SIZE; i++)
{
// Checks if table's i index position is not NULL
if (table[i] != NULL)
// Delete the data
delete table[i];
// Delete the complete table
delete[] table;
}// End of for loop
}// End of destructor
};// End of class
// main function definition
int main()
{
// Creates an object of class HashMap
HashMap hash;
// To store the key and value entered by the user
string key, value;
// To store user choice
int choice;
// Loops till user choice is not 4
while (true)
{
// Displays menu
cout<<" ----------------------"<<endl;
cout<<"Operations on Hash Table"<<endl;
cout<<" ----------------------"<<endl;
cout<<"1.Insert element into the table"<<endl;
cout<<"2.Search element from the key"<<endl;
cout<<"3.Delete element at a key"<<endl;
cout<<"4.Exit"<<endl;
// Accepts user choice
cout<<"Enter your choice: ";
cin>>choice;
// Checks the choice using switch and calls appropriate function
switch(choice)
{
case 1:
cout<<"Enter First name as value to be inserted: ";
cin>>value;
cout<<"Enter Last name as key at which element to be inserted: ";
cin>>key;
hash.Insert(key, value);
break;
case 2:
cout<<"Enter Last name as key of the element to be searched: ";
cin>>key;
if (hash.Search(key) == " ")
{
cout<<"No element found at key "<<key<<endl;
continue;
}
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}
break;
case 3:
cout<<"Enter Last name as key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
exit(1);
default:
cout<<" Enter correct option ";
}// End of switch - case
}// End of while loop
return 0;
}// End of main function
Sample Output:
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Jane
Enter Last name as key at which element to be inserted: Smith
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: John
Enter Last name as key at which element to be inserted: Do
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Susan
Enter Last name as key at which element to be inserted: Collins
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Bill
Enter Last name as key at which element to be inserted: Rodgers
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Eric
Enter Last name as key at which element to be inserted: Jones
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Bill
Enter Last name as key at which element to be inserted: Wright
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 1
Enter First name as value to be inserted: Mike
Enter Last name as key at which element to be inserted: Barder
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 2
Enter Last name as key of the element to be searched: Jones
Element at key Jones : Eric
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 3
Enter Last name as key of the element to be deleted: Jones
Element Deleted
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table
2.Search element from the key
3.Delete element at a key
4.Exit
Enter your choice: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.