Hash Function: Add the ASCII values of all characters in the key string and return the reminder obtained when divided by the size of the table. Example: If key = "Bob", and size = 53, we get (66 + 111 + 98) % 53 => 275 % 53 => 10. Collision Resolution Method: Linear Probe. The assignment consists of the following classes/files: - main.cpp (incomplete) - Student.h (given) - HashNode.h (given) - HashTable.h (given) - HashTable.cpp (incomplete) Read and understand the given code then write two functions: - insert hash - search hash
80 lines
2.0 KiB
C++
80 lines
2.0 KiB
C++
/*
|
|
CIS 22C
|
|
|
|
This program builds and searches a hash table.
|
|
|
|
Written by: Iurii Tatishchev
|
|
Reviewed & Modified by: Iurii Tatishchev
|
|
IDE: CLion
|
|
|
|
*/
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <cctype>
|
|
|
|
#include "HashTable.h"
|
|
|
|
using namespace std;
|
|
|
|
void buildHash(HashTable &hash);
|
|
|
|
void searchManager(HashTable &hash);
|
|
|
|
int main() {
|
|
HashTable hash;
|
|
string option;
|
|
|
|
buildHash(hash);
|
|
|
|
cout << "Load Factor: " << hash.getLoadFactor() << endl;
|
|
|
|
searchManager(hash);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/* **************************************************
|
|
This function builds a hash table with data from the keyboard.
|
|
It calls the insert() function that inserts the new data at the right location in the hash table.
|
|
An input line contains the gpa of a student follow by name (assume it is a single word name)
|
|
To stop reading enter "#"
|
|
************************************************** */
|
|
void buildHash(HashTable &hash) {
|
|
string line;
|
|
cout << "Enter gpa and name [# to stop reading]:" << endl;
|
|
getline(cin, line);
|
|
while (line != "#") {
|
|
stringstream temp(line); // create a stringstream named temp with data from line
|
|
double gpa;
|
|
string name;
|
|
temp >> gpa; // read gpa from temp
|
|
temp >> name; // read name from temp
|
|
Student newStu(name, gpa);
|
|
hash.insert(newStu);
|
|
getline(cin, line);
|
|
}
|
|
|
|
}
|
|
|
|
/* **************************************************
|
|
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(HashTable &hash) {
|
|
string name;
|
|
cout << "Enter name [# to stop searching]:" << endl;
|
|
getline(cin, name);
|
|
while (name != "#") {
|
|
Student item;
|
|
int nc;
|
|
if (hash.search(item, nc, name)) {
|
|
cout << item.getName() << " " << item.getGpa() << " (" << nc << " collisions!)" << endl;
|
|
} else {
|
|
cout << name << " not found!" << endl;
|
|
}
|
|
getline(cin, name);
|
|
}
|
|
|
|
}
|