PLEASE NOTE: USE C++ AND ALSO DESCRIBE YOUR CODES PLEASE. EARLIER ANSWER IS WRON
ID: 3842827 • Letter: P
Question
PLEASE NOTE: USE C++ AND ALSO DESCRIBE YOUR CODES PLEASE. EARLIER ANSWER IS WRONG, PLEASE DO NOT DO IF YOU DONOT KNOW. DONT WASTE MY QUESTIONS. DO IT FROM SCRATCH.
Please help me out, need ASAP. Please use c++ programming language. And also I need output and codes, please. Thanks in advance.
Object Orientated Programming or Procedural Programming are ok.
To receive full credit, programmers must apply coding conventions for code block indentation, comments describing methods/classes, white space between code blocks, and variable/method/class naming conventions.
Hard Code a Hash Map that stores has doubles as values and integers as keys that will hold 100 key,value pairs. For this example create unique key values (Do not use map class from STD library.) Create an iterator that will allow the program to return the key,value pairs.
Fill the HashMap with ~20 values and output them to the screen.
Explanation / Answer
The following code is the implementation of HashMap using singly linked list. Relevant comments are attached to the corresponding code lines.
/*
* Hard coding Hash map with Singly Linked List
*
*/
#include<iostream>
#include<cstdlib>
#include<string>
#include<cstdio>
using namespace std;
const int TABLE_SIZE = 100;//As suggested in the question
/*
* HashNode Class Declaration
*/
class HashNode
{
public:
int key;
double value;
HashNode* next;
HashNode(int key, double value)
{
this->key = key;
this->value = value;
this->next = NULL;
}
};
/*
* HashMap Class Declaration
*/
class HashMap
{
private:
HashNode** htable;//pointer to the hash node
public:
HashMap()
{
htable = new HashNode*[TABLE_SIZE];/* Initialising hash table*/
for (int i = 0; i < TABLE_SIZE; i++)
htable[i] = NULL;
}
~HashMap() /*Destructor*/
{
for (int i = 0; i < TABLE_SIZE; ++i)
{
HashNode* entry = htable[i];
while (entry != NULL)
{
HashNode* prev = entry;
entry = entry->next;
delete prev;
}
}
delete[] htable;
}
/*
* Hash Function
*/
int HashFunc(int key)
{
return key % TABLE_SIZE; /*Generating unique key*/
}
/*
* Insert Element at a key
*/
void Insert(int key, double value)
{
int hash_val = HashFunc(key);
HashNode* prev = NULL;
HashNode* entry = htable[hash_val];
while (entry != NULL)
{
prev = entry;
entry = entry->next;
}
if (entry == NULL)
{
entry = new HashNode(key, value);
if (prev == NULL)
{
htable[hash_val] = entry;
}
else
{
prev->next = entry;
}
}
else
{
entry->value = value;
}
}
/*
* Remove Element at a key
*/
void Remove(int key)
{
int hash_val = HashFunc(key);
HashNode* entry = htable[hash_val];
HashNode* prev = NULL;
if (entry == NULL || entry->key != key)
{
cout<<"No Element found at key "<<key<<endl;
return;
}
while (entry->next != NULL)
{
prev = entry;
entry = entry->next;
}
if (prev != NULL)
{
prev->next = entry->next;
}
delete entry;
cout<<"Element Deleted"<<endl;
}
/*Display Elements */
void Display()
{
cout<<"Key value pair";
cout<<i<<".";
cout<<prev->key;
/*
* Search Element at a key
*/
int Search(int key)
{
bool flag = false;
int hash_val = HashFunc(key);
HashNode* entry = htable[hash_val];
while (entry != NULL)
{
if (entry->key == key)
{
cout<<entry->value<<" ";
flag = true;
}
entry = entry->next;
}
if (!flag)
return -1;
}
};
/*
* Main Contains Menu
*/
int main()
{
HashMap hash;
double value;
int key;
int choice;
while (1)
{
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"<<endl;
cout<<"5.Exit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
int numberOfValues,k;
cout<<"Enter the number of elements to be inserted";
cin>>numberOfValues;
for(k=0;k<numberOfValues;k++)/*Inserting function is called */
{
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;
cout<<"Element at key "<<key<<" : ";
if (hash.Search(key) == -1)
{
cout<<"No element found at key "<<key<<endl;
continue;
}
break;
case 3:
cout<<"Enter key of the element to be deleted: ";
cin>>key;
hash.Remove(key);
break;
case 4:
hash.Display();
case 5:
exit(1);
default:
cout<<" Enter correct option ";
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.