Name the program for this assignment \" hashing.cpp .\" The purpose of this prog
ID: 3559397 • Letter: N
Question
Name the program for this assignment "hashing.cpp." The purpose of this program is to give you experience implementing a hash table, handling collisions, and using hashing with a linear function. Hashing is a method that enables access to table items (for adding, deleting, searching and so forth) in time that is relatively constant and independent of the items in the table. When we searched lists, implemented using linked lists and arrays, the time required to determine if an item was in the list, in the worst case, was proportional to the number of items in the list. Hashing uses a hash function to determine the location of an item. A problem with hashing occurs when the hash function returns the same value for two or more items (a hash function is a function that maps the search key of a table item into a location that will contain that item). The situation is referred to as a collision. A collision occurs when a hash function maps two or more distinct search keys into the same location.
In this assignment you will write a program that maintains the names (first and last names), addresses and phone numbers in an address book by using a hash table. Use the data file called "client_address_data.txt" to help you create the hash table. Also, you should be able to enter, delete, modify (names, addresses and phone numbers), or search the data stored in hash table based on the name. A client
Explanation / Answer
/* * C++ Program to Implement Hash Tables Chaining with List Heads */ #include #include #include #include #include #include #include using namespace std; const int TABLE_SIZE = 5; /* * Link List Class Declaration */ class LinkedHash { public: int key, value; LinkedHash *next; LinkedHash(int key, int value) { this->key = key; this->value = value; this->next = NULL; } }; /* * HashMap Class Declaration */ class HashMap { private: LinkedHash **htable; public: HashMap() { htable = new LinkedHash*[TABLE_SIZE]; for (int i = 0; i next != NULL) entry = entry->next; if (entry->key == key) entry->value = value; else entry->next = new LinkedHash(key, value); } } /* * Search Element at a key */ int Find(int key) { int hash_val = HashFunc(key); if (htable[hash_val] == NULL) return -1; else { LinkedHash *entry = htable[hash_val]; while (entry != NULL && entry->key != key) entry = entry->next; if (entry == NULL) return -1; else return entry->value; } } /* * Delete Element at a key */ void Delete(int key) { int hash_val = HashFunc(key); if (htable[hash_val] != NULL) { LinkedHash *entry = htable[hash_val]; LinkedHash *prev = NULL; while (entry->next != NULL && entry->key != key) { prev = entry; entry = entry->next; } if (entry->key == key) { if (prev == NULL) { LinkedHash *next = entry->next; delete entry; htable[hash_val] = next; } else { LinkedHash *next = entry->next; delete entry; prev->next = next; } } } } }; /* * Main Contains Menu */ int main() { HashMap hash; int key, value; int choice; while(1) { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.