📄 hash.h
字号:
template <class Type>
class hashTable {
private:
int numBuckets; // 哈希表的长度
int divisor; // 模值
List<Type> * buckets;
int hashFunction(const Type & k);
public:
hashTable(int num,int d) : numBuckets(num),divisor(d)
{ buckets = new List<Type> [num];}
void insert (const Type & k);
void deleteNode (const Type & k);
bool find (const Type & k);
Type *current;
};
/*
template <class Type>
Type hashTable <Type> ::hashFunction(const Type & k)
{
return (k%divisor);
}
*/
template <class Type>
bool hashTable <Type> ::find(const Type & k)
{ int i = hashFunction(k);
buckets[i].reset();
if (buckets[i].size == 0)
return false;
else
for (int j=0;j<buckets[i].size;j++)
{ if (buckets[i].data() == k) // 找到当前项,取出数值,并将当前指针指向它
{ current = &(buckets[i].data());
return true;
}
buckets[i].next();
}
return false;
}
template <class Type>
void hashTable <Type> ::insert(const Type & k)
{ int i = hashFunction(k);
buckets[i].insert(k);
}
template <class Type>
void hashTable <Type> ::deleteNode(const Type & k)
{ int i = hashFunction(k);
if (find(k))
buckets[i].deleteNode();
else
cout << "无法删除不存在的元素!"<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -