参考他人思路而写,具体可参考:http://blog.csdn.net/anialy/article/details/7620469
1 #ifndef _HASHTABLE_ 2 #define _HASHTABLE_ 3 #include<iostream> 4 using namespace std; 5 6 7 8 template<typename key_type,typename value_type> 9 struct HashNode{ 10 key_type key; 11 value_type value; 12 HashNode* next; 13 HashNode(key_type key,value_type value):key(key),value(value),next(NULL){} 14 HashNode& operator =(const HashNode& rhs){ 15 key = rhs.key; 16 value = rhs.value; 17 next = rhs.next; 18 return *this; 19 } 20 ~HashNode(){} 21 }; 22 23 template<typename key_type,typename value_type,typename Func> 24 class HashTable{ 25 public: 26 //enum{TableSize = 100}; 27 typedef HashNode<key_type,value_type>* pointer; 28 HashNode<key_type,value_type>** table; 29 unsigned long capacity; 30 Func GetKeyValue; 31 const value_type _null; 32 HashTable(Func func,const value_type& _null); 33 ~HashTable(); 34 void put(const HashNode<key_type,value_type>&); 35 value_type del(const key_type& key); 36 value_type getValue(const key_type& key); 37 }; 38 39 template<typename key_type,typename value_type,typename Func> 40 HashTable<key_type,value_type,Func>::HashTable(Func func,const value_type& _null):_null(_null){ 41 GetKeyValue = func; 42 capacity = 100000000; 43 table = new HashNode<key_type,value_type>*[capacity]; 44 for(unsigned long i=0;i<capacity;i++) 45 table[i]=NULL; 46 } 47 48 template<typename key_type,typename value_type,typename Func> 49 HashTable<key_type,value_type,Func>::~HashTable(){ 50 pointer node = NULL; 51 for(unsigned long i=0;i<capacity;i++){ 52 node = table[i]; 53 while(node!=NULL){ 54 pointer temp = node; 55 node = node->next; 56 delete temp; 57 temp = NULL; 58 } 59 } 60 delete[] table; 61 } 62 63 template<typename key_type,typename value_type,typename Func> 64 void HashTable<key_type,value_type,Func>::put(const HashNode<key_type,value_type>& node){ 65 pointer hashnode=NULL; 66 unsigned long index = GetKeyValue(node.key); 67 hashnode = table[index]; 68 if(hashnode == NULL){ 69 HashNode<key_type,value_type>* temp = new HashNode<key_type,value_type>(node.key,node.value); 70 table[index] = temp; 71 }else{ 72 HashNode<key_type,value_type>* temp = new HashNode<key_type,value_type>(node.key,node.value); 73 temp->next = hashnode; 74 table[index] = temp; 75 } 76 } 77 78 template<typename key_type,typename value_type,typename Func> 79 value_type HashTable<key_type,value_type,Func>::del(const key_type& key){ 80 unsigned long index = GetKeyValue(key); 81 pointer hashnode = table[index]; 82 pointer pre = NULL; 83 if(hashnode==NULL)return _null; 84 if(hashnode->key==key){ 85 table[index] = hashnode->next; 86 value_type revalue = hashnode->value; 87 delete hashnode; 88 table[index]=NULL; 89 return revalue; 90 } 91 while(hashnode->key!=key&&hashnode!=NULL){ 92 pre = hashnode; 93 hashnode = hashnode->next; 94 } 95 if(hashnode==NULL)return _null; 96 pre->next = hashnode->next; 97 value_type revalue = hashnode->value; 98 delete hashnode; 99 return revalue; 100 } 101 102 template<typename key_type,typename value_type,typename Func> 103 value_type HashTable<key_type,value_type,Func>::getValue(const key_type& key){ 104 unsigned long index = GetKeyValue(key); 105 pointer hashnode = table[index]; 106 if(hashnode==NULL)return _null; 107 while(hashnode->key!=key&&hashnode!=NULL)hashnode = hashnode->next; 108 if(hashnode==NULL)return _null; 109 return hashnode->value; 110 } 111 #endif
以上是头文件;
1 #include<iostream> 2 #include<time.h> 3 #include<string> 4 #include<cstdlib> 5 #include"hashtable.h" 6 using namespace std; 7 8 typedef unsigned long (*GetKeyValue)(const string& rhs); 9 unsigned long get_key(const string& key){ 10 unsigned long hash = 0; 11 unsigned long i=0; 12 while(i<key.length()) 13 { 14 // equivalent to: hash = 65599*hash + (*str++); 15 hash = key[i] + (hash << 6) + (hash << 16) - hash; 16 i++; 17 } 18 19 return (hash & 0x7FFFFFFF)>>8;//右移8位,是因为容易越界访问 20 } 21 22 int main(){ 23 24 //传入一个求哈希散列值的方法get_key 25 HashTable<string,string,GetKeyValue> hashMap(get_key,"NULL"); 26 for(int i = 0;i < 10000;i++){ 27 char *ckey = new char[20]; 28 char *cvalue = new char[20]; 29 ckey = itoa(i,ckey,8); 30 cvalue = itoa(i,cvalue,8); 31 string key(ckey); 32 string value(cvalue); 33 if(i == 67){ 34 key = "67"; 35 value = "hello hash No.67"; 36 } 37 HashNode<string,string> node1(key,value); 38 //插入该节点 39 hashMap.put(node1); 40 } 41 cout << "hashMap.GetValue(\"100\"): " << hashMap.getValue("100") << endl; 42 cout << "hashMap.Delete(\"67\"): " << hashMap.del("67") << endl; 43 cout << "hashMap.GetValue(\"67\"): " << hashMap.getValue("67") << endl; 44 return 0; 45 }
时间: 2024-10-13 12:26:01