Hashing (Linear Probing)
Note:
This code was written during a crunch period and isn't perfect. There will
be some errant spacing, some files will be
using namespace std,
etc. But it's all still usable and can be a
handy guideline if you're learning Data Structures.
#include <vector>
#include <map>
#include <string>
#include <sstream>
#include <algorithm>
struct HashTable
{
HashTable(int size)
{
table = std::vector<int>(size, -1);
}
void insert(int value)
{
int key = value % table.size();
int spacer = 0;
while (table[key] != -1 && spacer < table.size())
{
key = ((value % table.size()) + spacer) % table.size();
spacer++;
}
table[key] = value;
}
std::vector<int> table;
};
int main(int argc, char *argv[])
{
HashTable tb(10);
for (int x : nums)
tb.insert(x);
for (int x : tb.table)
std::cout << x << " ";
return 0;
}