📄 hashtable.h
字号:
#ifndef HashTable_H
#define HashTable_H
#include"Seqlist.h"
#include"LinkedList.h"
template<class T>
class HashTable
{
private:
int bucketNum;
SeqList< LinkedList<T> > bucket;
unsigned long (*hf)(const T& key);
public:
HashTable(int nbuckets,
unsigned long hashf(const T& key));
~HashTable();
void Insert(const T& key);
ListNode<T>* Find(T& key);
void Delete(const T& key);
void ClearList(void);
void Update(const T& key);
void Summary(char ch[],int freq[],int& m);
friend ostream& operator << (ostream& os, const HashTable<T>& h);
};
template<class T>
HashTable<T>::HashTable(int nbuckets,
unsigned long hashf(const T& key))
:bucketNum(nbuckets), bucket(bucketNum),hf(hashf)
{}
template<class T>
HashTable<T>::~HashTable()
{
ClearList();
}
template<class T>
void HashTable<T>::Insert(const T& key)
{
int pos = int(hf(key));
bucket[pos].InsertFront(key);
}
template<class T>
ListNode<T>* HashTable<T>::Find(T& key)
{
int pos = int(hf(key));
int j = bucket[pos].Find(key);
return bucket[pos].Locate(j);
}
template<class T>
void HashTable<T>::Delete(const T& key)
{
int pos = int(hf(key));
bucket[pos].DeleteNode(key);
}
template<class T>
void HashTable<T>::ClearList(void)
{
for(int i=0;i<bucketNum;i++)
bucket[i].ClearList();
}
template<class T>
void HashTable<T>::Update(const T& key)
{
int pos = int(hf(key));
int j = bucket[pos].Find(key);
(bucket[pos].Locate(j)->data)++;
}
template<class T>
ostream& operator << (ostream& os, const HashTable<T>& h)
{
for(int i=0;i<h.bucketNum;i++)
{
if(h.bucket[i].IsEmpty()) {os<<"bucket"<<" "<<i<<endl;continue;}
os<<"bucket"<<" "<<i<<endl;
os<<h.bucket[i];
}
return os;
}
template<class T>
void HashTable<T>::Summary(char ch[],int freq[],int& m)
{
int j=0;
for(int i=0;i<bucketNum;i++)
{
ListNode<T> *p = bucket[i].header;
while(p!=NULL){
ch[j] = (p->data).ch;
freq[j++] = (p->data).fre;
p = p->link;
m++;
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -