Question: Please write in C++, thank you! #include #include //srand, rand #include //clock_t, clock, CLOCKS_PER_SEC using namespace std; // implementing hash tables as an array of

Please write in C++, thank you!

Please write in C++, thank you! #include #include //srand, rand #include //clock_t,

#include

#include //srand, rand

#include //clock_t, clock, CLOCKS_PER_SEC

using namespace std;

// implementing hash tables as an array of linked lists

class Node{

private:

int data;

Node* nextNodePtr;

public:

Node(){}

void setData(int d){

data = d;

}

int getData(){

return data;

}

void setNextNodePtr(Node* nodePtr){

nextNodePtr = nodePtr;

}

Node* getNextNodePtr(){

return nextNodePtr;

}

};

class List{

private:

Node *headPtr;

public:

List(){

headPtr = new Node();

headPtr->setNextNodePtr(0);

}

Node* getHeadPtr(){

return headPtr;

}

bool isEmpty(){

if (headPtr->getNextNodePtr() == 0)

return true;

return false;

}

void insert(int data){

Node* currentNodePtr = headPtr->getNextNodePtr();

Node* prevNodePtr = headPtr;

while (currentNodePtr != 0){

prevNodePtr = currentNodePtr;

currentNodePtr = currentNodePtr->getNextNodePtr();

}

Node* newNodePtr = new Node();

newNodePtr->setData(data);

newNodePtr->setNextNodePtr(0);

prevNodePtr->setNextNodePtr(newNodePtr);

}

bool deleteElement(int deleteData){

Node* currentNodePtr = headPtr->getNextNodePtr();

Node* prevNodePtr = headPtr;

Node* nextNodePtr = headPtr;

while (currentNodePtr != 0){

if (currentNodePtr->getData() == deleteData){

nextNodePtr = currentNodePtr->getNextNodePtr();

prevNodePtr->setNextNodePtr(nextNodePtr);

return true;

}

prevNodePtr = currentNodePtr;

currentNodePtr = currentNodePtr->getNextNodePtr();

}

return false;

}

int countList(){

Node* currentNodePtr = headPtr->getNextNodePtr();

int numElements = 0;

while (currentNodePtr != 0){

numElements++;

currentNodePtr = currentNodePtr->getNextNodePtr();

}

return numElements;

}

void IterativePrint(){

Node* currentNodePtr = headPtr->getNextNodePtr();

while (currentNodePtr != 0){

cout getData()

currentNodePtr = currentNodePtr->getNextNodePtr();

}

cout

}

bool containsElement(int searchData){

Node* currentNodePtr = headPtr->getNextNodePtr();

while (currentNodePtr != 0){

if (currentNodePtr->getData() == searchData)

return true;

currentNodePtr = currentNodePtr->getNextNodePtr();

}

return false;

}

};

class Hashtable{

private:

List* listArray;

int tableSize;

public:

Hashtable(int size){

tableSize = size;

listArray = new List[size];

}

int getTableSize(){

return tableSize;

}

void insert(int data){

int hashIndex = data % tableSize;

listArray[hashIndex].insert(data);

}

void deleteElement(int data){

int hashIndex = data % tableSize;

while (listArray[hashIndex].deleteElement(data));

}

bool hasElement(int data){

int hashIndex = data % tableSize;

return listArray[hashIndex].containsElement(data);

}

void printHashTable(){

for (int hashIndex = 0; hashIndex

cout

listArray[hashIndex].IterativePrint();

}

}

};

int main(){

int numElements;

cout

cin >> numElements;

int maxValue;

cout

cin >> maxValue;

int hashTableSize;

cout

cin >> hashTableSize;

Hashtable hashTable(hashTableSize);

srand(time(NULL));

int array[numElements];

cout

for (int index = 0; index

array[index] = rand() % maxValue;

cout

hashTable.insert(array[index]);

}

cout

return 0;

}

Q3 - 15 pts) You are given the code for storing integer elements of an array in a Hashtable. Modify the code in such a way that you could print the number of occurrences of every unique element in the array in (n) time, where n is the number of elements in the array. You could modify any class (including the Node class) as well as add suitable member functions and/or modify existing member functions, if needed. Also, extend the main function to print the number of occurrences of every unique element in the array. Below is a sample screenshot of the output. Enter the number of elements you want to store in the hash table : 25 Enter the maximum value for an element: 15 Enter the size of the hash table: 7 Elements generated: 0 6 12 1 9 8 4 2 7 11 6 5 7 5 14 1 8 7 9 14 6 0 2 10 8 Elements and their number of occurrences 9, 2 7, 3 2 8, 3 9. 2 2, 2 10, 1 4, 1 11, 1 12, 1 5, 2 6, 3

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!