www.pudn.com > compressor.zip > HashTable.h
#ifndef HashTable_H #define HashTable_H #include"Seqlist.h" #include"LinkedList.h" templateclass HashTable { private: int bucketNum; SeqList< LinkedList > bucket; unsigned long (*hf)(const T& key); public: HashTable(int nbuckets, unsigned long hashf(const T& key)); ~HashTable(); void Insert(const T& key); ListNode * 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 & h); }; template HashTable ::HashTable(int nbuckets, unsigned long hashf(const T& key)) :bucketNum(nbuckets), bucket(bucketNum),hf(hashf) {} template HashTable ::~HashTable() { ClearList(); } template void HashTable ::Insert(const T& key) { int pos = int(hf(key)); bucket[pos].InsertFront(key); } template ListNode * HashTable ::Find(T& key) { int pos = int(hf(key)); int j = bucket[pos].Find(key); return bucket[pos].Locate(j); } template void HashTable ::Delete(const T& key) { int pos = int(hf(key)); bucket[pos].DeleteNode(key); } template void HashTable ::ClearList(void) { for(int i=0;i void HashTable ::Update(const T& key) { int pos = int(hf(key)); int j = bucket[pos].Find(key); (bucket[pos].Locate(j)->data)++; } template ostream& operator << (ostream& os, const HashTable & h) { for(int i=0;i void HashTable ::Summary(char ch[],int freq[],int& m) { int j=0; for(int i=0;i *p = bucket[i].header; while(p!=NULL){ ch[j] = (p->data).ch; freq[j++] = (p->data).fre; p = p->link; m++; } } } #endif