TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
linear_probing_hash_table.cpp File Reference

Storage mechanism using linear probing hash keys. More...

#include <iostream>
#include <vector>
Include dependency graph for linear_probing_hash_table.cpp:

Go to the source code of this file.

Classes

struct  linear_probing::Entry
 

Namespaces

namespace  linear_probing
 An implementation of hash table using linear probing algorithm.
 

Functions

bool linear_probing::putProber (const Entry &entry, int key)
 
bool linear_probing::searchingProber (const Entry &entry, int key)
 
void linear_probing::add (int key)
 
size_t linear_probing::hashFxn (int key)
 Hash a key. Uses the STL library's std::hash() function.
 
int linear_probing::linearProbe (int key, bool searching)
 
void linear_probing::display ()
 
void linear_probing::rehash ()
 
void linear_probing::remove (int key)
 
void linear_probing::addInfo (int key)
 
void linear_probing::removalInfo (int key)
 
int main ()
 

Variables

int linear_probing::notPresent
 
std::vector< Entrylinear_probing::table
 
int linear_probing::totalSize
 
int linear_probing::tomb = -1
 
int linear_probing::size
 
bool linear_probing::rehashing
 

Detailed Description

Storage mechanism using linear probing hash keys.

Author
achance6
Krishna Vedala
Note
The implementation can be optimized by using OOP style.

Definition in file linear_probing_hash_table.cpp.

Function Documentation

◆ main()

int main ( void )

Main function

Returns
0 on success

Definition at line 224 of file linear_probing_hash_table.cpp.

224 {
225 int cmd = 0, hash = 0, key = 0;
226 std::cout << "Enter the initial size of Hash Table. = ";
227 std::cin >> totalSize;
228 table = std::vector<Entry>(totalSize);
229 bool loop = true;
230 while (loop) {
231 std::cout << std::endl;
232 std::cout << "PLEASE CHOOSE -" << std::endl;
233 std::cout << "1. Add key. (Numeric only)" << std::endl;
234 std::cout << "2. Remove key." << std::endl;
235 std::cout << "3. Find key." << std::endl;
236 std::cout << "4. Generate Hash. (Numeric only)" << std::endl;
237 std::cout << "5. Display Hash table." << std::endl;
238 std::cout << "6. Exit." << std::endl;
239 std::cin >> cmd;
240 switch (cmd) {
241 case 1:
242 std::cout << "Enter key to add = ";
243 std::cin >> key;
245 break;
246 case 2:
247 std::cout << "Enter key to remove = ";
248 std::cin >> key;
250 break;
251 case 3: {
252 std::cout << "Enter key to search = ";
253 std::cin >> key;
254 Entry entry = table[linear_probing::linearProbe(key, true)];
255 if (entry.key == linear_probing::notPresent) {
256 std::cout << "Key not present";
257 }
258 break;
259 }
260 case 4:
261 std::cout << "Enter element to generate hash = ";
262 std::cin >> key;
263 std::cout << "Hash of " << key
264 << " is = " << linear_probing::hashFxn(key);
265 break;
266 case 5:
268 break;
269 default:
270 loop = false;
271 break;
272 // delete[] table;
273 }
274 std::cout << std::endl;
275 }
276 return 0;
277}
void * hash(const std::string &message)
Converts the string to bytestring and calls the main algorithm.
Definition md5.cpp:288
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
int linearProbe(int key, bool searching)
int key
key value