diff options
Diffstat (limited to 'Assign5/hash.cpp')
-rw-r--r-- | Assign5/hash.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/Assign5/hash.cpp b/Assign5/hash.cpp new file mode 100644 index 0000000..2e63984 --- /dev/null +++ b/Assign5/hash.cpp @@ -0,0 +1,78 @@ +#include<iostream> +#include"table.hpp" + +/* hash.cpp by Adam Carpenter - acarpent - acarpenter@email.wm.edu + +This program is a driver that utilizes the table.cpp hash table. It +reads in input from the command line (or fed in from file) and creates +an array containing the given items. It then creates hash table objects +and hashes the items into each of these objects using the four different +methods described in class: linear probing, quadratic probing, double +hashing, and separate chaining. It then calls for printing out all of +the hash tables. + +*/ + +void readInput(int inputArray[]) { + /* Reads input from command line or from a given file of keys + into an array of size 10 (the maximum number of input items. + */ + std::cout << "\nHash Tables!\n\n"; + std::cout << "Enter up to 10 positive integers to be hashed one at a time.\nWhen you are done, enter -1\n"; + int tmp = 0; + int count = 0; + + while (tmp != EMPTY && count < 10) { + std::cin >> tmp; + inputArray[count] = tmp; + count++; + } + + // Print entered list + std::cout << "\nEntered integers: "; + + for (int i = 0; i < 10; i++) { + + if (inputArray[i] != EMPTY) { + std::cout << inputArray[i] << " "; + } + + } + + std::cout << "\n"; +} + +int main() { + /* main is the primary driver of the hash table demonstration. + It is the caller of all of the subsequent functions and hash + table methods. + */ + int integerList[10] = {EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY}; + readInput(integerList); + + // Linear Probing + Table linearTable(false); + linearTable.hashLinear(integerList); + std::cout << "\nLinear Probing Hash Table: \n"; + linearTable.printTable(false); + + // Quadratic Probing + Table quadraticTable(false); + quadraticTable.hashQuad(integerList); + std::cout << "\nQuadratic Probing Hash Table: \n"; + quadraticTable.printTable(false); + + // Double Hashing + Table doubleTable(false); + doubleTable.hashDouble(integerList); + std::cout << "\nExtra Credit: Double Hashing Hash Table: \n"; + doubleTable.printTable(false); + + // Separate Chaining + Table chainTable(true); + chainTable.hashChaining(integerList); + std::cout << "\nExtra Credit: Separate Chaining Hash Table: \n"; + chainTable.printTable(true); + + return 0; +} |