Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
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:

Classes

struct  linear_probing::Entry
 

Namespaces

 linear_probing
 An implementation of hash table using linear probing algorithm.
 

Typedefs

using linear_probing::Entry = struct Entry
 

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. More...
 
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.

Function Documentation

◆ main()

int main ( void  )

Main function

Returns
0 on success
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 }
linear_probing::Entry::key
int key
key value
Definition: linear_probing_hash_table.cpp:37
linear_probing::Entry
Definition: linear_probing_hash_table.cpp:35
std::vector
STL class.
linear_probing::hashFxn
size_t hashFxn(int key)
Hash a key. Uses the STL library's std::hash() function.
Definition: linear_probing_hash_table.cpp:46
linear_probing::addInfo
void addInfo(int key)
Definition: linear_probing_hash_table.cpp:186
linear_probing::linearProbe
int linearProbe(int key, bool searching)
Definition: linear_probing_hash_table.cpp:55
std::cout
std::endl
T endl(T... args)
linear_probing::removalInfo
void removalInfo(int key)
Definition: linear_probing_hash_table.cpp:201
linear_probing::display
void display()
Definition: linear_probing_hash_table.cpp:120
std::cin