From d955a49de9cff055887edfba89fa6e8bbe983321 Mon Sep 17 00:00:00 2001 From: Krishna Vedala <7001608+kvedala@users.noreply.github.com> Date: Thu, 23 Jul 2020 18:16:56 -0400 Subject: [PATCH] fixed code fir linear_probing.... --- hashing/linear_probing_hash_table.cpp | 115 ++++++++++++++------------ 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/hashing/linear_probing_hash_table.cpp b/hashing/linear_probing_hash_table.cpp index 393504c1d..a125f016b 100644 --- a/hashing/linear_probing_hash_table.cpp +++ b/hashing/linear_probing_hash_table.cpp @@ -1,24 +1,30 @@ -// Copyright 2019 - -#include -#include +/** + * @file + * @author [achance6](https://github.com/achance6) + * @author [Krishna Vedala](https://github.com/kvedala) + * @brief + */ #include #include +#include using std::cin; using std::cout; using std::endl; using std::string; +namespace { // keep the code local to this file by assigning them to an unnamed + // namespace + // fwd declarations -struct Entry; +using Entry = struct Entry; bool putProber(Entry entry, int key); bool searchingProber(Entry entry, int key); void add(int key); // globals int notPresent; -struct Entry* table; +std::vector table; int totalSize; int tomb = -1; int size; @@ -31,18 +37,18 @@ struct Entry { }; // Hash a key -int hashFxn(int key) { +size_t hashFxn(int key) { std::hash hash; return hash(key); } // Performs linear probing to resolve collisions int linearProbe(int key, bool searching) { - int hash = static_cast(fabs(hashFxn(key))); + int hash = static_cast(hashFxn(key)); int i = 0; Entry entry; do { - int index = static_cast(fabs((hash + i) % totalSize)); + int index = static_cast((hash + i) % totalSize); entry = table[index]; if (searching) { if (entry.key == notPresent) { @@ -56,12 +62,14 @@ int linearProbe(int key, bool searching) { i++; } else { if (putProber(entry, key)) { - if (!rehashing) + if (!rehashing) { cout << "Spot found!" << endl; + } return index; } - if (!rehashing) + if (!rehashing) { cout << "Spot taken, looking at next" << endl; + } i++; } if (i == totalSize) { @@ -82,8 +90,9 @@ bool putProber(Entry entry, int key) { // Looks for a matching key bool searchingProber(Entry entry, int key) { - if (entry.key == key) + if (entry.key == key) { return true; + } return false; } @@ -108,27 +117,26 @@ void rehash() { // Necessary so wall of add info isn't printed all at once rehashing = true; int oldSize = totalSize; - Entry* oldTable = table; - // Really this should use the next prime number greater than totalSize * 2 - table = new Entry[totalSize * 2]; + std::vector oldTable(table); + // Really this should use the next prime number greater than totalSize * + // 2 totalSize *= 2; + table = std::vector(totalSize); for (int i = 0; i < oldSize; i++) { if (oldTable[i].key != -1 && oldTable[i].key != notPresent) { size--; // Size stays the same (add increments size) add(oldTable[i].key); } } - delete[] oldTable; + // delete[] oldTable; rehashing = false; cout << "Table was rehashed, new size is: " << totalSize << endl; } // Adds entry using linear probing. Checks for load factor here void add(int key) { - Entry* entry = new Entry(); - entry->key = key; int index = linearProbe(key, false); - table[index] = *entry; + table[index].key = key; // Load factor greater than 0.5 causes resizing if (++size / static_cast(totalSize) >= 0.5) { rehash(); @@ -152,7 +160,7 @@ void addInfo(int key) { display(); cout << endl; cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize - << " == " << fabs(hashFxn(key) % totalSize); + << " == " << hashFxn(key) % totalSize; cout << endl; add(key); cout << "New table: "; @@ -171,13 +179,14 @@ void removalInfo(int key) { cout << "New table: "; display(); } +} // namespace // I/O -int main(void) { - int cmd, hash, key; +int main() { + int cmd = 0, hash = 0, key = 0; cout << "Enter the initial size of Hash Table. = "; cin >> totalSize; - table = new Entry[totalSize]; + table = std::vector(totalSize); bool loop = true; while (loop) { system("pause"); @@ -191,37 +200,37 @@ int main(void) { cout << "6. Exit." << endl; cin >> cmd; switch (cmd) { - case 1: - cout << "Enter key to add = "; - cin >> key; - addInfo(key); - break; - case 2: - cout << "Enter key to remove = "; - cin >> key; - removalInfo(key); - break; - case 3: { - cout << "Enter key to search = "; - cin >> key; - Entry entry = table[linearProbe(key, true)]; - if (entry.key == notPresent) { - cout << "Key not present"; + case 1: + cout << "Enter key to add = "; + cin >> key; + addInfo(key); + break; + case 2: + cout << "Enter key to remove = "; + cin >> key; + removalInfo(key); + break; + case 3: { + cout << "Enter key to search = "; + cin >> key; + Entry entry = table[linearProbe(key, true)]; + if (entry.key == notPresent) { + cout << "Key not present"; + } + break; } - break; - } - case 4: - cout << "Enter element to generate hash = "; - cin >> key; - cout << "Hash of " << key << " is = " << fabs(hashFxn(key)); - break; - case 5: - display(); - break; - default: - loop = false; - break; - delete[] table; + case 4: + cout << "Enter element to generate hash = "; + cin >> key; + cout << "Hash of " << key << " is = " << hashFxn(key); + break; + case 5: + display(); + break; + default: + loop = false; + break; + // delete[] table; } cout << endl; }