mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
fixed code fir linear_probing....
This commit is contained in:
parent
abfe9ad081
commit
d955a49de9
@ -1,24 +1,30 @@
|
|||||||
// Copyright 2019
|
/**
|
||||||
|
* @file
|
||||||
#include <stdlib.h>
|
* @author [achance6](https://github.com/achance6)
|
||||||
#include <functional>
|
* @author [Krishna Vedala](https://github.com/kvedala)
|
||||||
|
* @brief
|
||||||
|
*/
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
using std::cin;
|
using std::cin;
|
||||||
using std::cout;
|
using std::cout;
|
||||||
using std::endl;
|
using std::endl;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
namespace { // keep the code local to this file by assigning them to an unnamed
|
||||||
|
// namespace
|
||||||
|
|
||||||
// fwd declarations
|
// fwd declarations
|
||||||
struct Entry;
|
using Entry = struct Entry;
|
||||||
bool putProber(Entry entry, int key);
|
bool putProber(Entry entry, int key);
|
||||||
bool searchingProber(Entry entry, int key);
|
bool searchingProber(Entry entry, int key);
|
||||||
void add(int key);
|
void add(int key);
|
||||||
|
|
||||||
// globals
|
// globals
|
||||||
int notPresent;
|
int notPresent;
|
||||||
struct Entry* table;
|
std::vector<Entry> table;
|
||||||
int totalSize;
|
int totalSize;
|
||||||
int tomb = -1;
|
int tomb = -1;
|
||||||
int size;
|
int size;
|
||||||
@ -31,18 +37,18 @@ struct Entry {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Hash a key
|
// Hash a key
|
||||||
int hashFxn(int key) {
|
size_t hashFxn(int key) {
|
||||||
std::hash<int> hash;
|
std::hash<int> hash;
|
||||||
return hash(key);
|
return hash(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Performs linear probing to resolve collisions
|
// Performs linear probing to resolve collisions
|
||||||
int linearProbe(int key, bool searching) {
|
int linearProbe(int key, bool searching) {
|
||||||
int hash = static_cast<int>(fabs(hashFxn(key)));
|
int hash = static_cast<int>(hashFxn(key));
|
||||||
int i = 0;
|
int i = 0;
|
||||||
Entry entry;
|
Entry entry;
|
||||||
do {
|
do {
|
||||||
int index = static_cast<int>(fabs((hash + i) % totalSize));
|
int index = static_cast<int>((hash + i) % totalSize);
|
||||||
entry = table[index];
|
entry = table[index];
|
||||||
if (searching) {
|
if (searching) {
|
||||||
if (entry.key == notPresent) {
|
if (entry.key == notPresent) {
|
||||||
@ -56,12 +62,14 @@ int linearProbe(int key, bool searching) {
|
|||||||
i++;
|
i++;
|
||||||
} else {
|
} else {
|
||||||
if (putProber(entry, key)) {
|
if (putProber(entry, key)) {
|
||||||
if (!rehashing)
|
if (!rehashing) {
|
||||||
cout << "Spot found!" << endl;
|
cout << "Spot found!" << endl;
|
||||||
|
}
|
||||||
return index;
|
return index;
|
||||||
}
|
}
|
||||||
if (!rehashing)
|
if (!rehashing) {
|
||||||
cout << "Spot taken, looking at next" << endl;
|
cout << "Spot taken, looking at next" << endl;
|
||||||
|
}
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
if (i == totalSize) {
|
if (i == totalSize) {
|
||||||
@ -82,8 +90,9 @@ bool putProber(Entry entry, int key) {
|
|||||||
|
|
||||||
// Looks for a matching key
|
// Looks for a matching key
|
||||||
bool searchingProber(Entry entry, int key) {
|
bool searchingProber(Entry entry, int key) {
|
||||||
if (entry.key == key)
|
if (entry.key == key) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,27 +117,26 @@ void rehash() {
|
|||||||
// Necessary so wall of add info isn't printed all at once
|
// Necessary so wall of add info isn't printed all at once
|
||||||
rehashing = true;
|
rehashing = true;
|
||||||
int oldSize = totalSize;
|
int oldSize = totalSize;
|
||||||
Entry* oldTable = table;
|
std::vector<Entry> oldTable(table);
|
||||||
// Really this should use the next prime number greater than totalSize * 2
|
// Really this should use the next prime number greater than totalSize *
|
||||||
table = new Entry[totalSize * 2];
|
// 2
|
||||||
totalSize *= 2;
|
totalSize *= 2;
|
||||||
|
table = std::vector<Entry>(totalSize);
|
||||||
for (int i = 0; i < oldSize; i++) {
|
for (int i = 0; i < oldSize; i++) {
|
||||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
|
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
|
||||||
size--; // Size stays the same (add increments size)
|
size--; // Size stays the same (add increments size)
|
||||||
add(oldTable[i].key);
|
add(oldTable[i].key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
delete[] oldTable;
|
// delete[] oldTable;
|
||||||
rehashing = false;
|
rehashing = false;
|
||||||
cout << "Table was rehashed, new size is: " << totalSize << endl;
|
cout << "Table was rehashed, new size is: " << totalSize << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds entry using linear probing. Checks for load factor here
|
// Adds entry using linear probing. Checks for load factor here
|
||||||
void add(int key) {
|
void add(int key) {
|
||||||
Entry* entry = new Entry();
|
|
||||||
entry->key = key;
|
|
||||||
int index = linearProbe(key, false);
|
int index = linearProbe(key, false);
|
||||||
table[index] = *entry;
|
table[index].key = key;
|
||||||
// Load factor greater than 0.5 causes resizing
|
// Load factor greater than 0.5 causes resizing
|
||||||
if (++size / static_cast<double>(totalSize) >= 0.5) {
|
if (++size / static_cast<double>(totalSize) >= 0.5) {
|
||||||
rehash();
|
rehash();
|
||||||
@ -152,7 +160,7 @@ void addInfo(int key) {
|
|||||||
display();
|
display();
|
||||||
cout << endl;
|
cout << endl;
|
||||||
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
cout << "hash of " << key << " is " << hashFxn(key) << " % " << totalSize
|
||||||
<< " == " << fabs(hashFxn(key) % totalSize);
|
<< " == " << hashFxn(key) % totalSize;
|
||||||
cout << endl;
|
cout << endl;
|
||||||
add(key);
|
add(key);
|
||||||
cout << "New table: ";
|
cout << "New table: ";
|
||||||
@ -171,13 +179,14 @@ void removalInfo(int key) {
|
|||||||
cout << "New table: ";
|
cout << "New table: ";
|
||||||
display();
|
display();
|
||||||
}
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
// I/O
|
// I/O
|
||||||
int main(void) {
|
int main() {
|
||||||
int cmd, hash, key;
|
int cmd = 0, hash = 0, key = 0;
|
||||||
cout << "Enter the initial size of Hash Table. = ";
|
cout << "Enter the initial size of Hash Table. = ";
|
||||||
cin >> totalSize;
|
cin >> totalSize;
|
||||||
table = new Entry[totalSize];
|
table = std::vector<Entry>(totalSize);
|
||||||
bool loop = true;
|
bool loop = true;
|
||||||
while (loop) {
|
while (loop) {
|
||||||
system("pause");
|
system("pause");
|
||||||
@ -191,37 +200,37 @@ int main(void) {
|
|||||||
cout << "6. Exit." << endl;
|
cout << "6. Exit." << endl;
|
||||||
cin >> cmd;
|
cin >> cmd;
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case 1:
|
case 1:
|
||||||
cout << "Enter key to add = ";
|
cout << "Enter key to add = ";
|
||||||
cin >> key;
|
cin >> key;
|
||||||
addInfo(key);
|
addInfo(key);
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
cout << "Enter key to remove = ";
|
cout << "Enter key to remove = ";
|
||||||
cin >> key;
|
cin >> key;
|
||||||
removalInfo(key);
|
removalInfo(key);
|
||||||
break;
|
break;
|
||||||
case 3: {
|
case 3: {
|
||||||
cout << "Enter key to search = ";
|
cout << "Enter key to search = ";
|
||||||
cin >> key;
|
cin >> key;
|
||||||
Entry entry = table[linearProbe(key, true)];
|
Entry entry = table[linearProbe(key, true)];
|
||||||
if (entry.key == notPresent) {
|
if (entry.key == notPresent) {
|
||||||
cout << "Key not present";
|
cout << "Key not present";
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
break;
|
case 4:
|
||||||
}
|
cout << "Enter element to generate hash = ";
|
||||||
case 4:
|
cin >> key;
|
||||||
cout << "Enter element to generate hash = ";
|
cout << "Hash of " << key << " is = " << hashFxn(key);
|
||||||
cin >> key;
|
break;
|
||||||
cout << "Hash of " << key << " is = " << fabs(hashFxn(key));
|
case 5:
|
||||||
break;
|
display();
|
||||||
case 5:
|
break;
|
||||||
display();
|
default:
|
||||||
break;
|
loop = false;
|
||||||
default:
|
break;
|
||||||
loop = false;
|
// delete[] table;
|
||||||
break;
|
|
||||||
delete[] table;
|
|
||||||
}
|
}
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user