-
Notifications
You must be signed in to change notification settings - Fork 0
/
lina_probing.cpp
101 lines (83 loc) · 2.99 KB
/
lina_probing.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <vector>
using namespace std;
class HashTable {
private:
vector<pair<int, string>> table; // table to hold key-value pairs
int size; // number of key-value pairs in the table
int capacity; // maximum number of key-value pairs the table can hold
int hash(int key); // hash function to map keys to indices in the table
int probe(int index); // linear probing function to find next available index in case of collision
public:
HashTable(int capacity); // constructor to initialize the hash table
void insert(int key, string value); // insert a key-value pair into the table
string get(int key); // get the value associated with a key in the table
void remove(int key); // remove a key-value pair from the table
void print(); // print the contents of the table
};
HashTable::HashTable(int capacity) {
this->table = vector<pair<int, string>>(capacity, make_pair(-1, ""));
this->size = 0;
this->capacity = capacity;
}
int HashTable::hash(int key) {
return key % this->capacity;
}
int HashTable::probe(int index) {
return (index + 1) % this->capacity;
}
void HashTable::insert(int key, string value) {
int index = hash(key);
// Linear probing until an empty or deleted slot is found
while (this->table[index].first != -1 && this->table[index].first != key) {
index = probe(index);
}
// Update value if key already exists
if (this->table[index].first == key) {
this->table[index].second = value;
}
// Add key-value pair to table if key does not exist
else {
this->table[index] = make_pair(key, value);
this->size++;
}
}
string HashTable::get(int key) {
int index = hash(key);
// Linear probing until key is found or an empty or deleted slot is encountered
while (this->table[index].first != -1) {
if (this->table[index].first == key) {
return this->table[index].second; // Return value if key is found
}
index = probe(index);
}
// Return empty string if key is not found
return "";
}
void HashTable::remove(int key) {
int index = hash(key);
// Linear probing until key is found or an empty or deleted slot is encountered
while (this->table[index].first != -1) {
if (this->table[index].first == key) {
this->table[index] = make_pair(-1, ""); // Mark slot as deleted
this->size--;
return;
}
index = probe(index);
}
}
void HashTable::print() {
for (int i = 0; i < this->capacity; i++) {
cout << "[" << i << "]: (" << this->table[i].first << ", " << this->table[i].second << ")" << endl;
}
}
int main() {
HashTable ht(5);
ht.insert(1, "apple");
ht.insert(2, "banana");
ht.insert(3, "cherry");
ht.insert(4, "date");
ht.insert(5, "elderberry");
ht.print();
cout << "Value for key 3: " << ht.get(3) << endl;
}