Hash Tables and functions

profileamadiya
Hashfile.pdf

// ASU CSE310 Assignment #5 Spring 2023 // Name of Author: // ASU ID: // Description: this is where you need to design functions on Hash hashTable, // such as hashInsert, hashDelete, hashSearch and hashDisplay // ---- is where you should add your own code #include "LinkedList.h" using namespace std; class Hash { private: LinkedList* hashTable; //hashTable is a one-dimensional array of LinkedList int m; //slots number of the hash table int tableSize; public: Hash(int size); ~Hash(); bool hashSearch(string firstName, string lastName, int id); bool hashInsert(string firstName, string lastName, int id, double salary); bool hashDelete(string firstName, string lastName, int id); int hashLoadFactor(); void hashDisplay(); int hashFunction(string key); }; //constructor Hash::Hash(int size) { m = size; hashTable = new LinkedList[m]; } //Destructor Hash::~Hash() { delete[] hashTable; }

//This function searches for a key inside the hash table and //return true if it is found and false otherwise //Note: key is the combination of firstName, lastName and id bool Hash::hashSearch(string firstName, string lastName, int id) { bool found = false; int hash_ind = hashFunction(firstName+lastName+to_string(id)) LinkedList* emp1 = hashTable[hash_ind].searchEmployee(id); if(emp1!=NULL && emp1->getFirstName() == firstName && emp1->getLastName()== lastName) { if (found == true) { cout << "\n" << left << setw(18) << firstName << setw(18) << lastName << setw(8) << id << " is found inside the hash table." << endl; } else { cout << "\n" << left << setw(18) << firstName << setw(18) << lastName << setw(8) << id << " is NOT found inside the hash table." << endl; } return found; } } //hashInsert inserts an Employee with the relevant info. into the hashTable. //it returns true if the data is inserted successfully and false otherwise bool Hash::hashInsert(string firstName, string lastName, int id, double salary) {

int hash_ind = hashFunction(firstName+lastName+to_string(id)); bool ins = hashTable[hash_ind].insertEmployee(firstName, lastName, id, salary); if(ins == true) { tableSize++; return true; } else { return false; } //---- //---- } //hashDelete deletes an Employee with the relevant key from the hashTable. //it returns true if it is deleted successfully and false otherwise //Note: key is the combination of firstName, lastName and id bool Hash::hashDelete(string firstName, string lastName, int id) { int hash_ind = hashFunction(firstName+lastName+to_string(id)); bool remove = hashTable[hash_ind].deleteEmployee(id); if(remove == true) { //---- //---- cout << "\n"; cout << setw(18) << firstName << setw(18) << lastName << setw(8) << id << " is deleted from hash table." << endl; } else { cout << "\n";

cout << setw(18) << firstName << setw(18) << lastName << setw(8) << id << " is NOT deleted from hash table." << endl; } //---- //---- //---- //---- return remove; } //This function computes your hash table real load factor //it is the longest linked list size int Hash::hashLoadFactor() { //---- //---- } //This function prints all elements from the hashTable. void Hash::hashDisplay() { int i = 0; for(i=0; i<tableSize; i++) { cout << "HashTable[" << i << "], size = " << linkedListSize[i] << endl; if(LinkedListSize[i] == 0) { continue; } Employee* temp1 = hashTable[i]; do { cout << temp1->firstName << "\t" << temp1->lastName << "\t" << temp1->id << "\t" << temp1->salary << endl;

temp1 = temp1->next; } while (temp1 != NULL); cout << endl; } //---- //---- } //This is the hash function you need to design. Given a //string key, the function should return the slot number //where we should hash the key to int Hash::hashFunction(string key) { int hash = 0; int ind; int i = 0; for(i=0; i< key.length(); i++) { hash = hash + (int) key[i]; } ind = hash % tableSize; return ind; //---- //---- }