Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello, I need as the code below to use hash tables and find the vaules from the file code example: #include #include #include #include using
Hello, I need as the code below to use hash tables and find the vaules from the file
code example:
#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 < TABLE_SIZE; i++) htable[i] = NULL; } ~HashMap() { 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; } /// Insert Element at a key void Insert(int key, int 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 "< next != NULL) { prev = entry; entry = entry->next; } if (prev != NULL) { prev->next = entry->next; } delete entry; cout<<"Element Deleted"< key == key) { cout< value<<" "; flag = true; } entry = entry->next; } if (!flag) return -1; } }; /// Main Contains Menu int main() { HashMap hash; int key, value; int choice; while (1) { cout<<" ----------------------"< >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; cout<<"Element at key "< >key; hash.Remove(key); break; case 4: exit(1); default: cout<<" Enter correct option "; } } return 0; }
Requeriments:
Write a program that provides for searching students by their ids using hash table. The program will first input students one by one from a file (insert.txt) and insert them into hash table. Then it will input students one by one from a text file (search.txt) and perform searches for them in the hash table. It will keep a record of searches when required and display the summary results.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started