Now that you have a working implementation of the HashNode and HashTable classes, you can convert them to templates, and update main.cpp to match with the new template classes. In order for everything to work you will also have to: - overload the == operator for the Student class - declare the hash function a friend function of the Student class - send the hash function as an argument to insert, remove, and search functions.
138 lines
4.4 KiB
C++
138 lines
4.4 KiB
C++
/*
|
|
CIS 22C
|
|
Hash Tables ADT - Linear Probe
|
|
Written By: Iurii Tatishchev
|
|
Reviewed & Modified by: Iurii Tatishchev
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include "HashTable.h"
|
|
#include "Student.h"
|
|
|
|
using namespace std;
|
|
|
|
void buildHash(HashTable<Student> &hash);
|
|
|
|
void searchManager(const HashTable<Student> &hash);
|
|
|
|
void deleteManager(HashTable<Student> &hash);
|
|
|
|
void insertManager(HashTable<Student> &hash);
|
|
|
|
int main() {
|
|
HashTable<Student> hash;
|
|
|
|
buildHash(hash);
|
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
|
searchManager(hash);
|
|
deleteManager(hash);
|
|
insertManager(hash);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* **************************************************
|
|
This function builds a hash table with data from an array
|
|
It calls the insert() function that inserts the new data at the right location in the hash table.
|
|
************************************************** */
|
|
void buildHash(HashTable<Student> &hash) {
|
|
|
|
Student list[] = {{"Tom", 2.5},
|
|
{"Bob", 3.2},
|
|
{"Boc", 3.2},
|
|
{"Linda", 3.9},
|
|
{"Tim", 4.0},
|
|
{"Vic", 3.9},
|
|
{"Ann", 3.5},
|
|
{"Dylan", 3.1},
|
|
{"obB", 2.2},
|
|
{"oBb", 3.7},
|
|
{"Bbo", 3.3},
|
|
{"bBo", 3.9},
|
|
{"boB", 2.3},
|
|
{"", 0}};
|
|
|
|
for (int i = 0; list[i].getName() != ""; i++) {
|
|
hash.insert(list[i], key_to_index);
|
|
}
|
|
|
|
}
|
|
|
|
/* **************************************************
|
|
This function searches a hash table with user provided data.
|
|
It calls the hash search function in a loop.
|
|
To stop searching enter "#"
|
|
************************************************** */
|
|
void searchManager(const HashTable<Student> &hash) {
|
|
cout << endl << "~*~ Test Search ~*~" << endl;
|
|
cout << "Enter name [# to stop searching]:" << endl;
|
|
string name;
|
|
getline(cin, name);
|
|
while (name != "#") {
|
|
Student item;
|
|
item.setName(name);
|
|
int nc = hash.search(item, item, key_to_index);
|
|
if (nc != -1) {
|
|
cout << item.getName() << " " << item.getGpa() << " (" << nc << " collisions!)" << endl;
|
|
} else {
|
|
cout << name << " not found!" << endl;
|
|
}
|
|
getline(cin, name);
|
|
}
|
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
|
}
|
|
|
|
/* **************************************************
|
|
This function deletes user provided data data from a hash table
|
|
It calls the hash delete function in a loop.
|
|
To stop deleting enter "#"
|
|
************************************************** */
|
|
void deleteManager(HashTable<Student> &hash) {
|
|
cout << endl << "~*~ Test Delete ~*~" << endl;
|
|
cout << "Enter name [# to stop deleting]:" << endl;
|
|
string name;
|
|
getline(cin, name);
|
|
while (name != "#") {
|
|
Student itemOut;
|
|
itemOut.setName(name);
|
|
if (hash.remove(itemOut, itemOut, key_to_index)) {
|
|
cout << itemOut.getName() << " " << itemOut.getGpa() << " - deleted!" << endl;
|
|
} else {
|
|
cout << name << " not found!" << endl;
|
|
}
|
|
getline(cin, name);
|
|
}
|
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
|
}
|
|
|
|
/* **************************************************
|
|
This function inserts user provided data into the hash table
|
|
It rejects duplicates.
|
|
It calls hash search and hash insert in a loop.
|
|
To stop getting user input enter "#"
|
|
************************************************** */
|
|
void insertManager(HashTable<Student> &hash) {
|
|
cout << endl << "~*~ Test Insert - reject duplicates ~*~" << endl;
|
|
cout << "Enter name [# to stop reading]:" << endl;
|
|
string name;
|
|
getline(cin, name);
|
|
while (name != "#") {
|
|
Student found;
|
|
found.setName(name);
|
|
if (hash.search(found, found, key_to_index) != -1) {
|
|
cout << "Duplicate key: " << found.getName() << " - rejected!" << endl;
|
|
} else {
|
|
cout << "Enter gpa:" << endl;
|
|
double gpa;
|
|
cin >> gpa;
|
|
cin.ignore();
|
|
Student newStudent(name, gpa);
|
|
hash.insert(newStudent, key_to_index);
|
|
cout << name << " - inserted (" << hash.search(found, newStudent, key_to_index) << " collisions)" << endl;
|
|
}
|
|
cout << "Enter name [# to stop reading]:" << endl;
|
|
getline(cin, name);
|
|
}
|
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
|
}
|