M為除留余數法的模數, ht是指向動態生成的一維數組指針, empty是標志數組的指針.
成員函數Find()在散列表中搜索與x關鍵字相同的元素. 若表中存在與x關鍵字值相同的元素, 則將其復制給x, pos指示該位置, 函數返回Success. 若表已滿, 函數返回Overflow. 若表未滿, 函數返回NotPresent, pos指示首次遇到的空值位置.
實現代碼:
~~~
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "stack"
#include "cmath"
#include "utility"
#include "map"
#include "set"
#include "vector"
#include "list"
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int NeverUsed = -100;
enum ResultCode { Underflow, Overflow, Success, Duplicate, NotPresent };
template <class T>
class DynamicSet
{
public:
virtual ResultCode Search(T &x) = 0;
virtual ResultCode Insert(T &x) = 0;
virtual ResultCode Remove(T &x) = 0;
/* data */
};
template <class T>
class HashTable: public DynamicSet<T>
{
public:
HashTable(int divisor = 11);
~HashTable() {
delete []ht;
delete []empty;
}
ResultCode Search(T &x);
ResultCode Insert(T &x);
ResultCode Remove(T &x);
void Output();
/* data */
private:
ResultCode Find(T &x, int &pos);
T h(T x);
int M;
T *ht;
bool *empty;
};
template <class T>
void HashTable<T>::Output()
{
for(int i = 0; i < M; ++i)
cout << i << ' ';
cout << endl;
for(int i = 0; i < M; ++i)
if(ht[i] == NeverUsed) cout << "NU" << ' ';
else cout << ht[i] << ' ';
cout << endl;
for(int i = 0; i < M; ++i)
if(empty[i]) cout << "T " << ' ';
else cout << "F " << ' ';
cout << endl;
}
template <class T>
T HashTable<T>::h(T x)
{
return x % 11;
}
template <class T>
HashTable<T>::HashTable(int divisor)
{
M = divisor;
ht = new T[M];
empty = new bool[M];
for(int i = 0; i < M; ++i)
empty[i] = true;
for(int i = 0; i < M; ++i)
ht[i] = NeverUsed;
}
template <class T>
ResultCode HashTable<T>::Find(T &x, int &pos)
{
pos = h(x); // h為散列函數, h(x) < M
int i = pos, j = -1;
do{
if(ht[pos] == NeverUsed && j == -1) j = pos; // 記錄首次遇到空值的位置
if(empty[pos]) break; // 表中沒有與x有相同關鍵字的元素
if(ht[pos] == x) { // ht[pos]的關鍵字與值與x的關鍵字值相同
x = ht[pos];
return Success; // 搜索成功, ht[pos]賦值給x
}
pos = (pos + 1) % M;
}while(pos != i); // 已搜索完整個散列表
if(j == -1) return Overflow; // 表已滿
pos = j; // 記錄首次遇到的空值位置
return NotPresent;
}
template <class T>
ResultCode HashTable<T>::Search(T &x)
{
int pos;
if(Find(x, pos) == Success) return Success;
return NotPresent;
}
template <class T>
ResultCode HashTable<T>::Insert(T &x)
{
int pos;
ResultCode reslt = Find(x, pos);
if(reslt == NotPresent) {
ht[pos] = x;
empty[pos] = false;
return Success;
}
if(reslt == Success) return Duplicate;
return Overflow;
}
template <class T>
ResultCode HashTable<T>::Remove(T &x)
{
int pos;
if(Find(x, pos) == Success) {
ht[pos] = NeverUsed;
return Success;
}
return NotPresent;
}
int main(int argc, char const *argv[])
{
HashTable<int> ht;
int x = 80; ht.Insert(x); ht.Output();
x = 80; ht.Insert(x); ht.Output();
x = 40; ht.Insert(x); ht.Output();
x = 65; ht.Insert(x); ht.Output();
x = 28; ht.Insert(x); ht.Output();
x = 24; ht.Insert(x); ht.Output();
x = 35; ht.Insert(x); ht.Output();
x = 58; ht.Insert(x); ht.Output();
return 0;
}
~~~
- 前言
- 線性表的順序表示:順序表ADT_SeqList
- 結點類和單鏈表ADT_SingleList
- 帶表頭結點的單鏈表ADT_HeaderList
- 堆棧的順序表示ADT_SeqStack
- 循環隊列ADT_SeqQueue
- 一維數組ADT_Array1D
- 稀疏矩陣ADT_SeqTriple
- 數據結構實驗1(順序表逆置以及刪除)
- 數據結構實驗1(一元多項式的相加和相乘)
- 二叉樹ADT_BinaryTree
- 優先隊列ADT_PrioQueue
- 堆ADT_Heap
- 數據結構實驗2(設計哈弗曼編碼和譯碼系統)
- ListSet_無序表搜索
- ListSet_有序表搜索
- ListSet_對半搜索的遞歸算法
- ListSet_對半搜索的迭代算法
- 二叉搜索樹ADT_BSTree
- 散列表ADT_HashTable
- 圖的鄰接矩陣實現_MGraph
- 圖的鄰接表實現_LGraph
- 數據結構實驗2(二叉鏈表實現二叉樹的基本運算)
- 數據結構實驗3(圖的DFS和BFS實現)
- 數據結構實驗3(飛機最少環城次數問題)
- 拓撲排序的實現_TopoSort
- 數據結構實驗4(排序算法的實現及性能分析)