Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by Taqui on 5/13/2017.
- //
- #include <iostream>
- #define PI 3.14159265359
- #include "Hash.h"
- using namespace std;
- Hash::Hash()
- {
- for (int i = 0; i < SIZE; i++)
- {
- hashArray[i] = NULL;
- }
- }
- int Hash::hashFunction(string key)
- {
- int hashIndex = 0;
- double sum = 0;
- for (int i = 0; i < key.length(); i++)
- {
- sum = (sum * PI) + (int) key[i];
- }
- hashIndex = (int) sum % SIZE;
- return hashIndex;
- }
- void Hash::addItem(string name, string phoneNumber)
- {
- int hashIndex = hashFunction(name);
- Item *newItem = new Item(name, phoneNumber);
- newItem->next = hashArray[hashIndex];
- hashArray[hashIndex] = newItem;
- }
- void Hash::showBucket(int index)
- {
- if (hashArray[index] != NULL)
- {
- cout << "\n---------BEGINNING OF THE BUCKET---------" << endl;
- for(Item *curr = hashArray[index]; curr != NULL; curr = curr->next)
- {
- cout << endl;
- curr->show();
- }
- cout << "\n------------END OF THE BUCKET------------" << endl;
- }
- else
- {
- cout << "\nBucket is empty." << endl;
- }
- }
- void Hash::showBucketFromKey(string key)
- {
- int hashIndex = hashFunction(key);
- Item *item = searchItem(key);
- if (item == NULL)
- {
- cout << "\nInvalid Key : Item could not be found." << endl;
- }
- else
- {
- showBucket(hashIndex);
- }
- }
- void Hash::showHashTable()
- {
- cout << "\n--------------------Hash Table Entries--------------------" << endl;
- for (int i = 0; i < Hash::SIZE; i++)
- {
- showBucket(i);
- }
- cout << "\n--------------------END OF THE TABLE--------------------" << endl;
- }
- Item* Hash::searchItem(string key)
- {
- cout << "\n------------Searching For "<< key <<"------------" << endl;
- int hashIndex = hashFunction(key);
- Item *bucketPtr = NULL;
- for (bucketPtr = hashArray[hashIndex]; bucketPtr != NULL; bucketPtr = bucketPtr->next)
- {
- if (bucketPtr->getName() == key)
- {
- cout << "Item " << key << " has been found at Index no: " << hashIndex << endl;
- /*cout << "\n#ITEM DETAILS :" << endl;
- bucketPtr->show();*/
- break;
- }
- }
- if (bucketPtr == NULL)
- {
- cout << "Item " << key << " could not be found in the Hash Table." << endl;
- }
- return bucketPtr;
- }
- void Hash::deleteItem(string key)
- {
- int hashIndex = hashFunction(key);
- Item *bucketPtr = hashArray[hashIndex];
- if (bucketPtr == NULL)
- {
- cout << "\nItem " << key << " could not be found to delete. In fact the bucket corresponding to the KEY: " << key <<" is empty." << endl;
- return;
- }
- else
- {
- if (bucketPtr->getName() == key)
- {
- hashArray[hashIndex] = bucketPtr->next;
- delete bucketPtr;
- cout << "\nItem " << key << " has been deleted." << endl;
- }
- else if (bucketPtr->next != NULL)
- {
- while(bucketPtr->next != NULL)
- {
- if (bucketPtr->next->getName() == key)
- {
- break;
- }
- bucketPtr = bucketPtr->next;
- }
- if (bucketPtr->next == NULL)
- {
- cout << "\nItem " << key << " could not be found to delete." << endl;
- }
- else
- {
- Item *deletePtr = bucketPtr->next;
- bucketPtr->next = deletePtr->next;
- delete deletePtr;
- cout << "\nItem " << key << " has been deleted." << endl;
- }
- }
- else
- {
- cout << "\nThere is only one item in this bucket( no: " << hashIndex << " ) and this item does not posses the same key as given( Key: " << key << " ). So, Item could not be found to delete." << endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment