9.13 Lab: Hashing - Linear Probe (insert, search, delete)

Reuse code from the previous lab and write new code as described below:

- search hash: Modify this function to return -1 if the target key is not found or the number of collisions for that key if found.

`int search(Student &target, string key);`

- remove hash: Create a new function to delete an item from the hash table.
- insert manager: inserts user provided data into the hash table and rejects duplicates.
This commit is contained in:
Iurii Tatishchev 2024-05-11 16:16:27 -07:00
parent 3d112946f5
commit b43f08aacc
Signed by: CaZzzer
GPG Key ID: 28BE602058C08557
3 changed files with 107 additions and 42 deletions

View File

@ -27,7 +27,7 @@ bool HashTable::insert(const Student &itemIn) {
return false; return false;
int pos = _hash(itemIn.getName()); int pos = _hash(itemIn.getName());
int collisions = 0; int collisions = 0;
while (hashAry[pos].getOccupied()) { while (hashAry[pos].getOccupied() == 1) {
++pos; ++pos;
++collisions; ++collisions;
pos = pos % hashSize; pos = pos % hashSize;
@ -43,7 +43,18 @@ bool HashTable::insert(const Student &itemIn) {
/*~*~*~* /*~*~*~*
hash delete - linear probe hash delete - linear probe
*~**/ *~**/
bool HashTable::remove(Student &itemOut) { bool HashTable::remove(Student &itemOut, string key) {
int pos = _hash(key);
for (int collisions = 0; collisions < count; collisions++) {
if (hashAry[pos].getOccupied() == 1 && hashAry[pos].getItem().getName() == key) {
itemOut = hashAry[pos].getItem();
// -1 means freed
hashAry[pos].setOccupied(-1);
count--;
return true;
}
pos = (pos + 1) % hashSize;
}
return false; return false;
} }
@ -56,16 +67,15 @@ bool HashTable::remove(Student &itemOut) {
- returns true - returns true
if not found, returns false if not found, returns false
*~**/ *~**/
bool HashTable::search(Student &itemOut, int &noCol, string key) { int HashTable::search(Student &itemOut, string key) const {
int pos = _hash(key); int pos = _hash(key);
for (int collisions = 0; collisions < count; collisions++) { for (int collisions = 0; collisions < count; collisions++) {
if (!hashAry[pos].getOccupied()) return false; if (hashAry[pos].getOccupied() == 0) return -1;
if (hashAry[pos].getItem().getName() == key) { if (hashAry[pos].getOccupied() == 1 && hashAry[pos].getItem().getName() == key) {
itemOut = hashAry[pos].getItem(); itemOut = hashAry[pos].getItem();
noCol = hashAry[pos].getNoCollisions(); return hashAry[pos].getNoCollisions();
return true;
} }
pos = (pos + 1) % hashSize; pos = (pos + 1) % hashSize;
} }
return false; return -1;
} }

View File

@ -40,9 +40,9 @@ public:
bool insert(const Student &itemIn); bool insert(const Student &itemIn);
bool remove(Student &itemOut); bool remove(Student &itemOut, string key);
bool search(Student &itemOut, int &noCol, string key); int search(Student &itemOut, string key) const;
private: private:
int _hash(string key) const; int _hash(string key) const;

View File

@ -1,16 +1,12 @@
/* /*
CIS 22C CIS 22C
Hashing - Linear Probe: insert, search, and delete
This program builds and searches a hash table. Written By: Iurii Tatishchev
Written by: Iurii Tatishchev
Reviewed & Modified by: Iurii Tatishchev Reviewed & Modified by: Iurii Tatishchev
IDE: CLion
*/ */
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <cctype>
#include "HashTable.h" #include "HashTable.h"
@ -18,40 +14,47 @@ using namespace std;
void buildHash(HashTable &hash); void buildHash(HashTable &hash);
void searchManager(HashTable &hash); void searchManager(const HashTable &hash);
void deleteManager(HashTable &hash);
void insertManager(HashTable &hash);
int main() { int main() {
HashTable hash; HashTable hash;
string option;
buildHash(hash); buildHash(hash);
cout << "Load Factor: " << hash.getLoadFactor() << endl; cout << "Load Factor: " << hash.getLoadFactor() << endl;
searchManager(hash); searchManager(hash);
deleteManager(hash);
insertManager(hash);
return 0; return 0;
} }
/* ************************************************** /* **************************************************
This function builds a hash table with data from the keyboard. 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. 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) { void buildHash(HashTable &hash) {
string line;
cout << "Enter gpa and name [# to stop reading]:" << endl; Student list[] = {{"Tom", 2.5},
getline(cin, line); {"Bob", 3.2},
while (line != "#") { {"Boc", 3.2},
stringstream temp(line); // create a stringstream named temp with data from line {"Linda", 3.9},
double gpa; {"Tim", 4.0},
string name; {"Vic", 3.9},
temp >> gpa; // read gpa from temp {"Ann", 3.5},
temp >> name; // read name from temp {"Dylan", 3.1},
Student newStu(name, gpa); {"obB", 2.2},
hash.insert(newStu); {"oBb", 3.7},
getline(cin, line); {"Bbo", 3.3},
{"bBo", 3.9},
{"boB", 2.3},
{"", 0}};
for (int i = 0; list[i].getName() != ""; i++) {
hash.insert(list[i]);
} }
} }
@ -61,19 +64,71 @@ This function searches a hash table with user provided data.
It calls the hash search function in a loop. It calls the hash search function in a loop.
To stop searching enter "#" To stop searching enter "#"
************************************************** */ ************************************************** */
void searchManager(HashTable &hash) { void searchManager(const HashTable &hash) {
string name; cout << endl << "~*~ Test Search ~*~" << endl;
cout << "Enter name [# to stop searching]:" << endl; cout << "Enter name [# to stop searching]:" << endl;
string name;
getline(cin, name); getline(cin, name);
while (name != "#") { while (name != "#") {
Student item; Student item;
int nc; int nc = hash.search(item, name);
if (hash.search(item, nc, name)) { if (nc != -1) {
cout << item.getName() << " " << item.getGpa() << " (" << nc << " collisions!)" << endl; cout << item.getName() << " " << item.getGpa() << " (" << nc << " collisions!)" << endl;
} else { } else {
cout << name << " not found!" << endl; cout << name << " not found!" << endl;
} }
getline(cin, name); getline(cin, name);
} }
}
/* **************************************************
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 &hash) {
cout << endl << "~*~ Test Delete ~*~" << endl;
cout << "Enter name [# to stop deleting]:" << endl;
string name;
getline(cin, name);
while (name != "#") {
Student itemOut;
if (hash.remove(itemOut, name)) {
cout << itemOut.getName() << " " << itemOut.getGpa() << " - deleted!" << endl;
} else {
cout << name << " not found!" << endl;
}
cout << "Load Factor: " << hash.getLoadFactor() << endl;
getline(cin, name);
}
}
/* **************************************************
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 &hash) {
cout << endl << "~*~ Test Insert - reject duplicates ~*~" << endl;
cout << "Enter name [# to stop reading]:" << endl;
string name;
getline(cin, name);
while (name != "#") {
Student found;
if (hash.search(found, name) != -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);
cout << name << " - inserted (" << hash.search(found, name) << " collisions)" << endl;
}
cout << "Load Factor: " << hash.getLoadFactor() << endl;
cout << "Enter name [# to stop reading]:" << endl;
getline(cin, name);
}
} }