c++版本
#include<iostream>
#include<stdlib.h>
using namespace std;
class BadInput {
public:
BadInput() {}
};
class NoMem {
public:
NoMem() {}
};
template<class E, class K>
class HashTable {
public:
HashTable(int divisor = 11);
~HashTable() {delete [] ht;
delete [] empty;}
bool Search(const K& k, E& e) const;
HashTable<E,K>& Insert(const E& e);
void Output();
private:
int hSearch(const K& k) const;
int D;
E *ht;
bool *empty;
};
template<class E, class K>
HashTable<E,K>::HashTable(int divisor)
{
D = divisor;
ht = new E [D];
empty = new bool [D];
for (int i = 0; i < D; i++)
empty[i] = true;
}
template<class E, class K>
int HashTable<E,K>::hSearch(const K& k) const
{
int i = k % D;
int j = i;
do {
if (empty[j] || ht[j] == k) return j;
j = (j + 1) % D;
} while (j != i);
return j;
}
template<class E, class K>
bool HashTable<E,K>::Search(const K& k, E& e) const
{
int b = hSearch(k);
if (empty[b] || ht[b] != k) return false;
e = ht[b];
return true;
}
template<class E, class K>
HashTable<E,K>& HashTable<E,K>::Insert(const E& e)
{
K k = e;
int b = hSearch(k);
if (empty[b]) {empty[b] = false;
ht[b] = e;
return *this;}
if (ht[b] == k) throw BadInput();
throw NoMem();
return *this;
}
template<class E, class K>
void HashTable<E,K>::Output()
{
for (int i = 0; i< D; i++) {
if (empty[i]) cout << "empty" << endl;
else cout << ht[i] << endl;}
}
class element {
friend void main(void);
public:
operator long() const {return key;}
private:
int data;
long key;
};
void main(void)
{
HashTable<element, long> h(11);
element e;
e.key = 80;
h.Insert(e);
e.key = 40;
h.Insert(e);
e.key = 65;
h.Insert(e);
h.Output();
e.key = 58;
h.Insert(e);
e.key = 24;
h.Insert(e);
cout << ' ' << endl;
h.Output();
e.key = 2;
h.Insert(e);
e.key = 13;
h.Insert(e);
e.key = 46;
h.Insert(e);
e.key = 16;
h.Insert(e);
e.key = 7;
h.Insert(e);
e.key = 21;
h.Insert(e);
cout << ' ' << endl;
h.Output();
e.key = 99;
try {h.Insert(e);}
catch (NoMem)
{cout << " No memory for 99" << endl;}
}
Inhabitable(链表散列)
#ifndef ChainedHashTable_
#define ChainedHashTable_
#include <iostream.h>
#include <stdlib.h>
#include "sochain.h"
template<class E, class K>
class ChainHashTable {
public:
ChainHashTable(int divisor = 11)
{D = divisor;
ht = new SortedChain<E,K> [D];}
~ChainHashTable() {delete [] ht;}
bool Search(const K& k, E& e) const
{return ht[k % D].Search(k, e);}
ChainHashTable<E,K>& Insert(const E& e)
{ht[e % D].DistinctInsert(e);
return *this;}
ChainHashTable<E,K>& Delete(const K& k, E& e)
{ht[k % D].Delete(k, e);
return *this;}
void Output() const;
private:
int D;
SortedChain<E,K> *ht;
};
template<class E, class K>
void ChainHashTable<E,K>::Output() const
{
SortedChain<E,K> c;
for (int i = 0; i < D; i++) {
cout << "Chain " << i << ' ';
ht[i].Output(cout);
cout << endl;}
}
#endif
#include <iostream.h>
#include "chash.h"
class element {
friend void main(void);
public:
operator long() const {return key;}
element& operator =(long y)
{key = y; return *this;}
private:
int data;
long key;
};
ChainHashTable<element, long> h(11);
element e;
void main(void)
{
e.key = 80;
h.Insert(e);
e.key = 40;
h.Insert(e);
e.key = 65;
h.Insert(e);
e.key = 58;
h.Insert(e);
e.key = 24;
h.Insert(e);
e.key = 2;
h.Insert(e);
e.key = 13;
h.Insert(e);
e.key = 46;
h.Insert(e);
e.key = 16;
h.Insert(e);
e.key = 7;
h.Insert(e);
e.key = 21;
h.Insert(e);
h.Delete(2,e);
h.Delete(16,e);
h.Delete(24,e);
h.Delete(80,e);
h.Output();
}
Hash_STL
http://www.cnblogs.com/CheeseZH/p/5176970.html