mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-24 10:33:10 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -2,51 +2,39 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct Node
|
||||
{
|
||||
struct Node {
|
||||
int data;
|
||||
struct Node *next;
|
||||
} * head[100], *curr;
|
||||
|
||||
void init()
|
||||
{
|
||||
void init() {
|
||||
for (int i = 0; i < 100; i++) head[i] = NULL;
|
||||
}
|
||||
|
||||
void add(int x, int h)
|
||||
{
|
||||
void add(int x, int h) {
|
||||
struct Node *temp = new Node;
|
||||
temp->data = x;
|
||||
temp->next = NULL;
|
||||
if (!head[h])
|
||||
{
|
||||
if (!head[h]) {
|
||||
head[h] = temp;
|
||||
curr = head[h];
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
curr = head[h];
|
||||
while (curr->next) curr = curr->next;
|
||||
curr->next = temp;
|
||||
}
|
||||
}
|
||||
|
||||
void display(int mod)
|
||||
{
|
||||
void display(int mod) {
|
||||
struct Node *temp;
|
||||
int i;
|
||||
for (i = 0; i < mod; i++)
|
||||
{
|
||||
if (!head[i])
|
||||
{
|
||||
for (i = 0; i < mod; i++) {
|
||||
if (!head[i]) {
|
||||
cout << "Key " << i << " is empty" << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << "Key " << i << " has values = ";
|
||||
temp = head[i];
|
||||
while (temp->next)
|
||||
{
|
||||
while (temp->next) {
|
||||
cout << temp->data << " ";
|
||||
temp = temp->next;
|
||||
}
|
||||
@@ -58,19 +46,16 @@ void display(int mod)
|
||||
|
||||
int hash(int x, int mod) { return x % mod; }
|
||||
|
||||
void find(int x, int h)
|
||||
{
|
||||
void find(int x, int h) {
|
||||
struct Node *temp = head[h];
|
||||
if (!head[h])
|
||||
{
|
||||
if (!head[h]) {
|
||||
cout << "Element not found";
|
||||
return;
|
||||
}
|
||||
while (temp->data != x && temp->next) temp = temp->next;
|
||||
if (temp->next)
|
||||
cout << "Element found";
|
||||
else
|
||||
{
|
||||
else {
|
||||
if (temp->data == x)
|
||||
cout << "Element found";
|
||||
else
|
||||
@@ -78,15 +63,13 @@ void find(int x, int h)
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
init();
|
||||
int c, x, mod, h;
|
||||
cout << "Enter the size of Hash Table. = ";
|
||||
cin >> mod;
|
||||
bool loop = true;
|
||||
while (loop)
|
||||
{
|
||||
while (loop) {
|
||||
cout << endl;
|
||||
cout << "PLEASE CHOOSE -" << endl;
|
||||
cout << "1. Add element." << endl;
|
||||
@@ -95,8 +78,7 @@ int main(void)
|
||||
cout << "4. Display Hash table." << endl;
|
||||
cout << "5. Exit." << endl;
|
||||
cin >> c;
|
||||
switch (c)
|
||||
{
|
||||
switch (c) {
|
||||
case 1:
|
||||
cout << "Enter element to add = ";
|
||||
cin >> x;
|
||||
|
||||
@@ -25,55 +25,44 @@ int size;
|
||||
bool rehashing;
|
||||
|
||||
// Node that holds key
|
||||
struct Entry
|
||||
{
|
||||
struct Entry {
|
||||
explicit Entry(int key = notPresent) : key(key) {}
|
||||
int key;
|
||||
};
|
||||
|
||||
// Hash a key
|
||||
int hashFxn(int key)
|
||||
{
|
||||
int hashFxn(int key) {
|
||||
std::hash<int> hash;
|
||||
return hash(key);
|
||||
}
|
||||
|
||||
// Used for second hash function
|
||||
int otherHashFxn(int key)
|
||||
{
|
||||
int otherHashFxn(int key) {
|
||||
std::hash<int> hash;
|
||||
return 1 + (7 - (hash(key) % 7));
|
||||
}
|
||||
|
||||
// Performs double hashing to resolve collisions
|
||||
int doubleHash(int key, bool searching)
|
||||
{
|
||||
int doubleHash(int key, bool searching) {
|
||||
int hash = static_cast<int>(fabs(hashFxn(key)));
|
||||
int i = 0;
|
||||
Entry entry;
|
||||
do
|
||||
{
|
||||
do {
|
||||
int index = static_cast<int>(fabs((hash + (i * otherHashFxn(key))))) %
|
||||
totalSize;
|
||||
entry = table[index];
|
||||
if (searching)
|
||||
{
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (searching) {
|
||||
if (entry.key == notPresent) {
|
||||
return notPresent;
|
||||
}
|
||||
if (searchingProber(entry, key))
|
||||
{
|
||||
if (searchingProber(entry, key)) {
|
||||
cout << "Found key!" << endl;
|
||||
return index;
|
||||
}
|
||||
cout << "Found tombstone or equal hash, checking next" << endl;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (putProber(entry, key))
|
||||
{
|
||||
} else {
|
||||
if (putProber(entry, key)) {
|
||||
if (!rehashing)
|
||||
cout << "Spot found!" << endl;
|
||||
return index;
|
||||
@@ -87,8 +76,7 @@ int doubleHash(int key, bool searching)
|
||||
<< ")" << endl;
|
||||
i++;
|
||||
}
|
||||
if (i == totalSize * 100)
|
||||
{
|
||||
if (i == totalSize * 100) {
|
||||
cout << "DoubleHash probe failed" << endl;
|
||||
return notPresent;
|
||||
}
|
||||
@@ -97,38 +85,28 @@ int doubleHash(int key, bool searching)
|
||||
}
|
||||
|
||||
// Finds empty spot
|
||||
bool putProber(Entry entry, int key)
|
||||
{
|
||||
if (entry.key == notPresent || entry.key == tomb)
|
||||
{
|
||||
bool putProber(Entry entry, int key) {
|
||||
if (entry.key == notPresent || entry.key == tomb) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Looks for a matching key
|
||||
bool searchingProber(Entry entry, int key)
|
||||
{
|
||||
bool searchingProber(Entry entry, int key) {
|
||||
if (entry.key == key)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Displays the table
|
||||
void display()
|
||||
{
|
||||
for (int i = 0; i < totalSize; i++)
|
||||
{
|
||||
if (table[i].key == notPresent)
|
||||
{
|
||||
void display() {
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
if (table[i].key == notPresent) {
|
||||
cout << " Empty ";
|
||||
}
|
||||
else if (table[i].key == tomb)
|
||||
{
|
||||
} else if (table[i].key == tomb) {
|
||||
cout << " Tomb ";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << " ";
|
||||
cout << table[i].key;
|
||||
cout << " ";
|
||||
@@ -138,8 +116,7 @@ void display()
|
||||
}
|
||||
|
||||
// Rehashes the table into a bigger table
|
||||
void rehash()
|
||||
{
|
||||
void rehash() {
|
||||
// Necessary so wall of add info isn't printed all at once
|
||||
rehashing = true;
|
||||
int oldSize = totalSize;
|
||||
@@ -147,10 +124,8 @@ void rehash()
|
||||
// Really this should use the next prime number greater than totalSize * 2
|
||||
table = new Entry[totalSize * 2];
|
||||
totalSize *= 2;
|
||||
for (int i = 0; i < oldSize; i++)
|
||||
{
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent)
|
||||
{
|
||||
for (int i = 0; i < oldSize; i++) {
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
|
||||
size--; // Size stays the same (add increments size)
|
||||
add(oldTable[i].key);
|
||||
}
|
||||
@@ -161,25 +136,21 @@ void rehash()
|
||||
}
|
||||
|
||||
// Checks for load factor here
|
||||
void add(int key)
|
||||
{
|
||||
void add(int key) {
|
||||
Entry* entry = new Entry();
|
||||
entry->key = key;
|
||||
int index = doubleHash(key, false);
|
||||
table[index] = *entry;
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Removes key. Leaves tombstone upon removal.
|
||||
void remove(int key)
|
||||
{
|
||||
void remove(int key) {
|
||||
int index = doubleHash(key, true);
|
||||
if (index == notPresent)
|
||||
{
|
||||
if (index == notPresent) {
|
||||
cout << "key not found" << endl;
|
||||
}
|
||||
table[index].key = tomb;
|
||||
@@ -188,8 +159,7 @@ void remove(int key)
|
||||
}
|
||||
|
||||
// Information about the adding process
|
||||
void addInfo(int key)
|
||||
{
|
||||
void addInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -202,8 +172,7 @@ void addInfo(int key)
|
||||
}
|
||||
|
||||
// Information about removal process
|
||||
void removalInfo(int key)
|
||||
{
|
||||
void removalInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -216,15 +185,13 @@ void removalInfo(int key)
|
||||
}
|
||||
|
||||
// I/O
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
int cmd, hash, key;
|
||||
cout << "Enter the initial size of Hash Table. = ";
|
||||
cin >> totalSize;
|
||||
table = new Entry[totalSize];
|
||||
bool loop = true;
|
||||
while (loop)
|
||||
{
|
||||
while (loop) {
|
||||
system("pause");
|
||||
cout << endl;
|
||||
cout << "PLEASE CHOOSE -" << endl;
|
||||
@@ -235,8 +202,7 @@ int main(void)
|
||||
cout << "5. Display Hash table." << endl;
|
||||
cout << "6. Exit." << endl;
|
||||
cin >> cmd;
|
||||
switch (cmd)
|
||||
{
|
||||
switch (cmd) {
|
||||
case 1:
|
||||
cout << "Enter key to add = ";
|
||||
cin >> key;
|
||||
@@ -247,13 +213,11 @@ int main(void)
|
||||
cin >> key;
|
||||
removalInfo(key);
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
case 3: {
|
||||
cout << "Enter key to search = ";
|
||||
cin >> key;
|
||||
Entry entry = table[doubleHash(key, true)];
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (entry.key == notPresent) {
|
||||
cout << "Key not present";
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -25,47 +25,37 @@ int size;
|
||||
bool rehashing;
|
||||
|
||||
// Node that holds key
|
||||
struct Entry
|
||||
{
|
||||
struct Entry {
|
||||
explicit Entry(int key = notPresent) : key(key) {}
|
||||
int key;
|
||||
};
|
||||
|
||||
// Hash a key
|
||||
int hashFxn(int key)
|
||||
{
|
||||
int hashFxn(int key) {
|
||||
std::hash<int> hash;
|
||||
return hash(key);
|
||||
}
|
||||
|
||||
// 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 i = 0;
|
||||
Entry entry;
|
||||
do
|
||||
{
|
||||
do {
|
||||
int index = static_cast<int>(fabs((hash + i) % totalSize));
|
||||
entry = table[index];
|
||||
if (searching)
|
||||
{
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (searching) {
|
||||
if (entry.key == notPresent) {
|
||||
return notPresent;
|
||||
}
|
||||
if (searchingProber(entry, key))
|
||||
{
|
||||
if (searchingProber(entry, key)) {
|
||||
cout << "Found key!" << endl;
|
||||
return index;
|
||||
}
|
||||
cout << "Found tombstone or equal hash, checking next" << endl;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (putProber(entry, key))
|
||||
{
|
||||
} else {
|
||||
if (putProber(entry, key)) {
|
||||
if (!rehashing)
|
||||
cout << "Spot found!" << endl;
|
||||
return index;
|
||||
@@ -74,8 +64,7 @@ int linearProbe(int key, bool searching)
|
||||
cout << "Spot taken, looking at next" << endl;
|
||||
i++;
|
||||
}
|
||||
if (i == totalSize)
|
||||
{
|
||||
if (i == totalSize) {
|
||||
cout << "Linear probe failed" << endl;
|
||||
return notPresent;
|
||||
}
|
||||
@@ -84,38 +73,28 @@ int linearProbe(int key, bool searching)
|
||||
}
|
||||
|
||||
// Finds empty spot
|
||||
bool putProber(Entry entry, int key)
|
||||
{
|
||||
if (entry.key == notPresent || entry.key == tomb)
|
||||
{
|
||||
bool putProber(Entry entry, int key) {
|
||||
if (entry.key == notPresent || entry.key == tomb) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Looks for a matching key
|
||||
bool searchingProber(Entry entry, int key)
|
||||
{
|
||||
bool searchingProber(Entry entry, int key) {
|
||||
if (entry.key == key)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Displays the table
|
||||
void display()
|
||||
{
|
||||
for (int i = 0; i < totalSize; i++)
|
||||
{
|
||||
if (table[i].key == notPresent)
|
||||
{
|
||||
void display() {
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
if (table[i].key == notPresent) {
|
||||
cout << " Empty ";
|
||||
}
|
||||
else if (table[i].key == tomb)
|
||||
{
|
||||
} else if (table[i].key == tomb) {
|
||||
cout << " Tomb ";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << " ";
|
||||
cout << table[i].key;
|
||||
cout << " ";
|
||||
@@ -125,8 +104,7 @@ void display()
|
||||
}
|
||||
|
||||
// Rehashes the table into a bigger table
|
||||
void rehash()
|
||||
{
|
||||
void rehash() {
|
||||
// Necessary so wall of add info isn't printed all at once
|
||||
rehashing = true;
|
||||
int oldSize = totalSize;
|
||||
@@ -134,10 +112,8 @@ void rehash()
|
||||
// Really this should use the next prime number greater than totalSize * 2
|
||||
table = new Entry[totalSize * 2];
|
||||
totalSize *= 2;
|
||||
for (int i = 0; i < oldSize; i++)
|
||||
{
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent)
|
||||
{
|
||||
for (int i = 0; i < oldSize; i++) {
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
|
||||
size--; // Size stays the same (add increments size)
|
||||
add(oldTable[i].key);
|
||||
}
|
||||
@@ -148,25 +124,21 @@ void rehash()
|
||||
}
|
||||
|
||||
// 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);
|
||||
table[index] = *entry;
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Removes key. Leaves tombstone upon removal.
|
||||
void remove(int key)
|
||||
{
|
||||
void remove(int key) {
|
||||
int index = linearProbe(key, true);
|
||||
if (index == notPresent)
|
||||
{
|
||||
if (index == notPresent) {
|
||||
cout << "key not found" << endl;
|
||||
}
|
||||
cout << "Removal Successful, leaving tomb" << endl;
|
||||
@@ -175,8 +147,7 @@ void remove(int key)
|
||||
}
|
||||
|
||||
// Information about the adding process
|
||||
void addInfo(int key)
|
||||
{
|
||||
void addInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -189,8 +160,7 @@ void addInfo(int key)
|
||||
}
|
||||
|
||||
// Information about removal process
|
||||
void removalInfo(int key)
|
||||
{
|
||||
void removalInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -203,15 +173,13 @@ void removalInfo(int key)
|
||||
}
|
||||
|
||||
// I/O
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
int cmd, hash, key;
|
||||
cout << "Enter the initial size of Hash Table. = ";
|
||||
cin >> totalSize;
|
||||
table = new Entry[totalSize];
|
||||
bool loop = true;
|
||||
while (loop)
|
||||
{
|
||||
while (loop) {
|
||||
system("pause");
|
||||
cout << endl;
|
||||
cout << "PLEASE CHOOSE -" << endl;
|
||||
@@ -222,8 +190,7 @@ int main(void)
|
||||
cout << "5. Display Hash table." << endl;
|
||||
cout << "6. Exit." << endl;
|
||||
cin >> cmd;
|
||||
switch (cmd)
|
||||
{
|
||||
switch (cmd) {
|
||||
case 1:
|
||||
cout << "Enter key to add = ";
|
||||
cin >> key;
|
||||
@@ -234,13 +201,11 @@ int main(void)
|
||||
cin >> key;
|
||||
removalInfo(key);
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
case 3: {
|
||||
cout << "Enter key to search = ";
|
||||
cin >> key;
|
||||
Entry entry = table[linearProbe(key, true)];
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (entry.key == notPresent) {
|
||||
cout << "Key not present";
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -26,54 +26,43 @@ int size;
|
||||
bool rehashing;
|
||||
|
||||
// Node that holds key
|
||||
struct Entry
|
||||
{
|
||||
struct Entry {
|
||||
explicit Entry(int key = notPresent) : key(key) {}
|
||||
int key;
|
||||
};
|
||||
|
||||
// Hash a key
|
||||
int hashFxn(int key)
|
||||
{
|
||||
int hashFxn(int key) {
|
||||
std::hash<int> hash;
|
||||
return hash(key);
|
||||
}
|
||||
|
||||
// Performs quadratic probing to resolve collisions
|
||||
int quadraticProbe(int key, bool searching)
|
||||
{
|
||||
int quadraticProbe(int key, bool searching) {
|
||||
int hash = static_cast<int>(fabs(hashFxn(key)));
|
||||
int i = 0;
|
||||
Entry entry;
|
||||
do
|
||||
{
|
||||
do {
|
||||
int index = std::round(fabs(
|
||||
(hash + static_cast<int>(std::round(std::pow(i, 2)))) % totalSize));
|
||||
entry = table[index];
|
||||
if (searching)
|
||||
{
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (searching) {
|
||||
if (entry.key == notPresent) {
|
||||
return notPresent;
|
||||
}
|
||||
if (searchingProber(entry, key))
|
||||
{
|
||||
if (searchingProber(entry, key)) {
|
||||
cout << "Found key!" << endl;
|
||||
return index;
|
||||
}
|
||||
cout << "Found tombstone or equal hash, checking next" << endl;
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (putProber(entry, key))
|
||||
{
|
||||
} else {
|
||||
if (putProber(entry, key)) {
|
||||
if (!rehashing)
|
||||
cout << "Spot found!" << endl;
|
||||
return index;
|
||||
}
|
||||
if (!rehashing)
|
||||
{
|
||||
if (!rehashing) {
|
||||
cout << "Spot taken, looking at next (next index = "
|
||||
<< std::round(fabs((hash + static_cast<int>(std::round(
|
||||
std::pow(i + 1, 2)))) %
|
||||
@@ -82,8 +71,7 @@ int quadraticProbe(int key, bool searching)
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (i == totalSize * 100)
|
||||
{
|
||||
if (i == totalSize * 100) {
|
||||
cout << "Quadratic probe failed (infinite loop)" << endl;
|
||||
return notPresent;
|
||||
}
|
||||
@@ -92,26 +80,22 @@ int quadraticProbe(int key, bool searching)
|
||||
}
|
||||
|
||||
// Finds empty spot
|
||||
bool putProber(Entry entry, int key)
|
||||
{
|
||||
if (entry.key == notPresent || entry.key == tomb)
|
||||
{
|
||||
bool putProber(Entry entry, int key) {
|
||||
if (entry.key == notPresent || entry.key == tomb) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Looks for a matching key
|
||||
bool searchingProber(Entry entry, int key)
|
||||
{
|
||||
bool searchingProber(Entry entry, int key) {
|
||||
if (entry.key == key)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Helper
|
||||
Entry find(int key)
|
||||
{
|
||||
Entry find(int key) {
|
||||
int index = quadraticProbe(key, true);
|
||||
if (index == notPresent)
|
||||
return Entry();
|
||||
@@ -119,20 +103,13 @@ Entry find(int key)
|
||||
}
|
||||
|
||||
// Displays the table
|
||||
void display()
|
||||
{
|
||||
for (int i = 0; i < totalSize; i++)
|
||||
{
|
||||
if (table[i].key == notPresent)
|
||||
{
|
||||
void display() {
|
||||
for (int i = 0; i < totalSize; i++) {
|
||||
if (table[i].key == notPresent) {
|
||||
cout << " Empty ";
|
||||
}
|
||||
else if (table[i].key == tomb)
|
||||
{
|
||||
} else if (table[i].key == tomb) {
|
||||
cout << " Tomb ";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
cout << " ";
|
||||
cout << table[i].key;
|
||||
cout << " ";
|
||||
@@ -142,8 +119,7 @@ void display()
|
||||
}
|
||||
|
||||
// Rehashes the table into a bigger table
|
||||
void rehash()
|
||||
{
|
||||
void rehash() {
|
||||
// Necessary so wall of add info isn't printed all at once
|
||||
rehashing = true;
|
||||
int oldSize = totalSize;
|
||||
@@ -151,10 +127,8 @@ void rehash()
|
||||
// Really this should use the next prime number greater than totalSize * 2
|
||||
table = new Entry[totalSize * 2];
|
||||
totalSize *= 2;
|
||||
for (int i = 0; i < oldSize; i++)
|
||||
{
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent)
|
||||
{
|
||||
for (int i = 0; i < oldSize; i++) {
|
||||
if (oldTable[i].key != -1 && oldTable[i].key != notPresent) {
|
||||
size--; // Size stays the same (add increments size)
|
||||
add(oldTable[i].key);
|
||||
}
|
||||
@@ -165,25 +139,21 @@ void rehash()
|
||||
}
|
||||
|
||||
// Checks for load factor here
|
||||
void add(int key)
|
||||
{
|
||||
void add(int key) {
|
||||
Entry* entry = new Entry();
|
||||
entry->key = key;
|
||||
int index = quadraticProbe(key, false);
|
||||
table[index] = *entry;
|
||||
// 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();
|
||||
}
|
||||
}
|
||||
|
||||
// Removes key. Leaves tombstone upon removal.
|
||||
void remove(int key)
|
||||
{
|
||||
void remove(int key) {
|
||||
int index = quadraticProbe(key, true);
|
||||
if (index == notPresent)
|
||||
{
|
||||
if (index == notPresent) {
|
||||
cout << "key not found" << endl;
|
||||
}
|
||||
table[index].key = tomb;
|
||||
@@ -192,8 +162,7 @@ void remove(int key)
|
||||
}
|
||||
|
||||
// Information about the adding process
|
||||
void addInfo(int key)
|
||||
{
|
||||
void addInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -206,8 +175,7 @@ void addInfo(int key)
|
||||
}
|
||||
|
||||
// Information about removal process
|
||||
void removalInfo(int key)
|
||||
{
|
||||
void removalInfo(int key) {
|
||||
cout << "Initial table: ";
|
||||
display();
|
||||
cout << endl;
|
||||
@@ -220,15 +188,13 @@ void removalInfo(int key)
|
||||
}
|
||||
|
||||
// I/O
|
||||
int main(void)
|
||||
{
|
||||
int main(void) {
|
||||
int cmd, hash, key;
|
||||
cout << "Enter the initial size of Hash Table. = ";
|
||||
cin >> totalSize;
|
||||
table = new Entry[totalSize];
|
||||
bool loop = true;
|
||||
while (loop)
|
||||
{
|
||||
while (loop) {
|
||||
system("pause");
|
||||
cout << endl;
|
||||
cout << "PLEASE CHOOSE -" << endl;
|
||||
@@ -239,8 +205,7 @@ int main(void)
|
||||
cout << "5. Display Hash table." << endl;
|
||||
cout << "6. Exit." << endl;
|
||||
cin >> cmd;
|
||||
switch (cmd)
|
||||
{
|
||||
switch (cmd) {
|
||||
case 1:
|
||||
cout << "Enter key to add = ";
|
||||
cin >> key;
|
||||
@@ -251,13 +216,11 @@ int main(void)
|
||||
cin >> key;
|
||||
removalInfo(key);
|
||||
break;
|
||||
case 3:
|
||||
{
|
||||
case 3: {
|
||||
cout << "Enter key to search = ";
|
||||
cin >> key;
|
||||
Entry entry = table[quadraticProbe(key, true)];
|
||||
if (entry.key == notPresent)
|
||||
{
|
||||
if (entry.key == notPresent) {
|
||||
cout << "Key not present";
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user