need help on c++ homework. Use the code below to help update the task above Full
ID: 3710783 • Letter: N
Question
need help on c++ homework.
Use the code below to help update the task above
Full Functioning Legacy Code That Implements Hash Map Between Integer key value pairs.
(int key, int value)
Copy and Paste the Code and Execute
Determine how the code is operating,
Task 1 requires a (“String” key, “string” value),
#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;
}
Here is how the execution should perform and look like below. The is the teachers excution this is how he wants it.
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
// File Name: HashMapString.cpp
#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 valueFirstName;
// To store the key as last name
string keyLastName;
// Parameterized constructor to assign parameter values to data member
HashEntry(string keyLastName, string valueFirstName)
{
this->valueFirstName = valueFirstName;
this->keyLastName = keyLastName;
}// 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]->keyLastName != 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]->keyLastName != 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]->valueFirstName;
}// 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]->keyLastName == 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;
}// End of else
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
// Function to display information
void displayTable()
{
// 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)
cout<<" Key = "<<table[i]->keyLastName<<" Value = "<<table[i]->valueFirstName;
}// End of function
};// 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.Display the table."<<endl;
cout<<"5.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;
}// End of if condition
else
{
cout<<"Element at key "<<key<<" : ";
cout<<hash.Search(key)<<endl;
}// End of else
break;
case 3:
cout<<"Enter Last name as key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
hash.displayTable();
break;
case 5:
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.Display the table.
5.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.Display the table.
5.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.Display the table.
5.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.Display the table.
5.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.Display the table.
5.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.Display the table.
5.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.Display the table.
5.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: Bader
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 4
Key = Bader Value = Mike
Key = Collins Value = Susan
Key = Do Value = John
Key = Jones Value = Eric
Key = Rodgers Value = Bill
Key = Smith Value = Jane
Key = Wright Value = Bill
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 2
Enter Last name as key of the element to be searched: check
No element found at key check
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 2
Enter Last name as key of the element to be searched: Do
Element at key Do : John
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 2
Enter Last name as key of the element to be searched: Bader
Element at key Bader : Mike
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 3
Enter Last name as key of the element to be deleted: del
No Element found at key del
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 3
Enter Last name as key of the element to be deleted: Do
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.Display the table.
5.Exit
Enter your choice: 4
Key = Bader Value = Mike
Key = Collins Value = Susan
Key = Jones Value = Eric
Key = Rodgers Value = Bill
Key = Smith Value = Jane
Key = Wright Value = Bill
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 53
Enter correct option.
----------------------
Operations on Hash Table
----------------------
1.Insert element into the table.
2.Search element from the key.
3.Delete element at a key.
4.Display the table.
5.Exit
Enter your choice: 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.