哈希表C++实现--hash_table


1、hash_table.h

#include <iostream>
  3 #include <string>
  4 #include <string.h>
  5 #include <vector>
  6 #include <stdio.h>
  7 using namespace std;
  8 
  9 struct table_node
 10 {
 11     int _key;
 12     string _value;
 13     struct table_node *next;
 14     table_node(const int key,const string &value)
 15         :_key(key),_value(value),next(NULL)
 16     {}
 17 };
 18 
 19 
 20 class hash_table
 21 {
 22     private:
 23         vector<table_node*> vector_node;
 24         size_t size;
 25     private:
 26         size_t fun_index(const int key)
 27         {
 28             return key % vector_node.size();
 29         }
 30         size_t get_size()
 31         {
 32             const int len=10;
 33             static size_t arr[len]={53,97,193,389,769,1543,3079,6151,12289,24593};
 34             static int i=0;
 35             return arr[i++];
 36         }
 37         void init_hash_table()
 38         { 
 39             if(vector_node.size()==size)
 40             {
 41                 vector<table_node*> new_node;
 42                 size_t new_size=get_size();
 43                 new_node.resize(new_size);
 44                 //////////
 45                 //再把之前vector的数组拷贝到新的vector中
 46                 for(int i=0; i<vector_node.size(); ++i)
 47                 {
 48                     table_node *cur=vector_node[i];
 49 
 50                     while(cur !=NULL)
 51                     {
 52                         size_t index=fun_index(cur->_key);
 53                         table_node *tmp=cur;
 54                         cur=cur->next;
 55 
 56                         table_node *value=new_node[index];
 57                         if(value==NULL)
 58                             new_node[index]=tmp;
 59                         else
 60                         {
 61                             tmp->next=value->next;
 62                             value->next=tmp;
 63                         }
 64                     }
 65                 }
 66                 vector_node.swap(new_node);
 67             }
 68         }
 69     public:
 70         hash_table(size_t sz=0):size(sz)
 71         {}
 72         void insert(const int key,const string &value)
 73         {
 74             init_hash_table();
 75 
 76             table_node *tmp=new table_node(key,value);
 77 
 78             size_t index=fun_index(key);
 79             table_node *cur=vector_node[index];
 80         
 81             if(cur==NULL)
 82                 vector_node[index]=tmp;
 83             else if(cur !=NULL)
 84             {
 85                 tmp->next=cur->next;
 86                 cur->next=tmp;
 87             }
 88             size++;//在这让size++的目的是为了让负载因子为一,即一个下表下只挂一个数据
 89         }
 90         void show()
 91         {
 92             for(int i=0; i<vector_node.size(); ++i)
 93             {
 94                 cout<<"第"<<i+1<<"个:";
 95                 table_node *cur=vector_node[i];
 96                 while(cur !=NULL)
 97                 {
 98                     cout<<cur->_value<<"-->";
 99                     cur=cur->next;
100                 }
101                 cout<<" NULL"<<endl;
102             }
103         }
104         void remove(const int &key)
105         {
106             size_t index=fun_index(key);
107             table_node *cur=vector_node[index];
108             if(cur !=NULL)
109             {
110                 if(cur->next==NULL)
111                 {
112                     delete vector_node[index];
113                     vector_node[index]=NULL;
114                 }
115                 else
116                 {
117                     table_node *tmp=cur->next;
118                     cur->next=tmp->next;
119                     delete tmp;
120                 }
121             }
122         }
123 };  



2、

#include "hash_table.h"
  3 
  4 void fun_hash_table()
  5 {
  6     hash_table t;
  7     t.insert(2,"1-hello");
  8     t.insert(2,"2-hello");
  9     t.insert(2,"3-hello");
 10     t.insert(2,"4-hello");
 11     t.insert(3,"world");
 12     t.insert(4,"Hello");
 13     t.insert(5,"hello");
 14     t.show();
 15 }
 16 int main()
 17 {
 18     fun_hash_table();
 19     return 0;
 20 }
~               


你可能感兴趣的:(哈希表C++实现,hash_table)