Skip to content

Instantly share code, notes, and snippets.

@orhandemirel
Created January 13, 2011 12:56
Show Gist options
  • Save orhandemirel/777817 to your computer and use it in GitHub Desktop.
Save orhandemirel/777817 to your computer and use it in GitHub Desktop.
// Author: Selcuk Orhan Demirel
// Date: 01/13/2011
// Simple implementation of a HashTable in C++
// Uses an array of linklists to form a hashtable.
#include <string>
#include <list> // link list
using namespace std;
#define SIZE 20
class Entry
{
public:
string key;
string value;
};
class HashTable
{
public:
void add(string key, string value);
string find(string key);
private:
int hashFunc(string key);
list<Entry> table[SIZE];
};
// adds a key-value pair to hash table
void HashTable::add(string key, string value)
{
int val = hashFunc(key);
Entry e;
e.key = key;
e.value = value;
table[val].push_back(e);
}
// find a given key in hastable and return the associated value.
string HashTable::find(string key)
{
int val = hashFunc(key);
list<Entry>::iterator it;
for(it = table[val].begin(); it != table[val].end(); it++)
{
if(it->key == key)
return it->value;
}
return NULL;
}
// hash function
int HashTable::hashFunc(string key)
{
int val = 0;
for(int i=0; i<(int)key.length();i++)
{
int letter = key[i] - 96;
val = ((val * 27) + letter) % SIZE; // using Horner's method
}
return val;
}
// Author: Selcuk Orhan Demirel
// Date: 01/13/2011
// Test of HashTable Class
#include <iostream>
#include "hashtable.h"
using namespace std;
int main()
{
HashTable ht;
ht.add("first","Orhan");
ht.add("second","Richie");
ht.add("third","Stan");
cout << ht.find("second") << endl;
system("PAUSE");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment