Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Coding in C++ Your task is to build a hash table and use singly linked list to a

ID: 3721121 • Letter: C

Question

Coding in C++

Your task is to build a hash table and use singly linked list to address collisions. The objects that your list must hold are stocks consisting of a String 'Name', unsigned int 'Code', and int 'Price' and of course an object array (Hint: it would be helpful if your array is type node *inventory[MAX SIZE]; Where node is an object which holds a Stock and a Node* next.) From your main program insert 500 stocks into your table, which can hold 2000 stocks, by creating a hash function that will return your the index where the stock was inserted. Your hash function should reduce collisions as much as possible, however, if you do encounter a collision use singly linked lists to address it. Finally write a short function that analyses your collision rate (you can accomplish this by dividing the total number of stocks by the spaces filled in the table ABC 0001 13.25 LNV 0 NULL 50.25 CYS 0002 100.5 2 NULL CTX 1997 89.2 1997 NULL 1998 NULL 1999 NULL

Explanation / Answer

/* * C++ Program to Implement Hash Tables chaining * with Singly Linked Lists */ #include #include #include #include using namespace std; const int TABLE_SIZE = 128; /* * HashNode Class Declaration */ class HashNode { public: . int key; int value; HashNode* next; HashNode(int key, int value) { this->key = key; this->value = value; this->next = NULL; } }; /* * HashMap Class Declaration */ class HashMap { private: HashNode** htable; public: HashMap() { htable = new HashNode*[TABLE_SIZE]; for (int i = 0; i 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