Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38732
  • 博文数量: 7
  • 博客积分: 154
  • 博客等级: 入伍新兵
  • 技术积分: 71
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-14 21:23
文章分类

全部博文(7)

文章存档

2012年(7)

我的朋友

分类: C/C++

2012-06-10 23:25:16

hashtable的最主要问题就是碰撞冲突的解决,常用的方法一般是开放地址法和链表法,这里主要用的是开放地址法。

最基本的hashtable实现

点击(此处)折叠或打开

  1. // HashTable.cpp : 定义控制台应用程序的入口点。
  2. //开放地址法,简单hashtable

  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <hash_map>
  6. using namespace std;

  7. const int HashSize=13;

  8. class HashTable
  9. {
  10. private:
  11.     int *hashtable;
  12. public:
  13.     //int *hashtable;
  14.     HashTable(){
  15.         hashtable=new int[HashSize];
  16.         if(hashtable==NULL){
  17.             cerr<<"hashtable init failed"<<endl;
  18.             exit(0);
  19.         }
  20.         for(int i=0 ; i<HashSize ; i++)
  21.             hashtable[i]=-1;
  22.     }
  23.     ~HashTable(){
  24.         delete []hashtable;
  25.     }

  26.     unsigned HashValue(unsigned value){
  27.         int key;
  28.         key=(unsigned)(value % HashSize);
  29.         if(hashtable[key]!=-1)
  30.             return HashValue(key+1);
  31.         return key;
  32.     }

  33.     void HashInsert(unsigned value){
  34.         int key=HashValue(value);
  35.         hashtable[key]=value;
  36.     }

  37.     int HashFind(unsigned value){
  38.         int key=(unsigned)(value % HashSize);
  39.         int i=0;
  40.         while(hashtable[key]!=-1 && i++<HashSize){
  41.             if(hashtable[key]==value)
  42.                 return key;
  43.             else
  44.                 key=(key+1)%HashSize;
  45.         }
  46.         return -1;
  47.     }
  48. };


  49. int _tmain(int argc, _TCHAR* argv[])
  50. {
  51.     int array[HashSize]={19,14,23,1,68,20,84,27,55,11,10,79};
  52.     HashTable h;
  53.     for(int i=0 ; i<HashSize-1 ; i++){
  54.         h.HashInsert(array[i]);
  55.     }
  56.     for(int k=0 ; k<HashSize-1 ; k++){
  57.         cout<<array[k]<<":"<<h.HashFind(array[k])<<endl;
  58.     }
  59.     return 0;
  60. }

使用hashtable对文件中的字符统计

点击(此处)折叠或打开

  1. // HashForWord.cpp : 定义控制台应用程序的入口点。
  2. //

  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;

  7. const int HashSize=15;

  8. typedef struct HashNode
  9. {
  10.     char *key;
  11.     int count;
  12. }hashNode;

  13. class HashTable
  14. {
  15. private:
  16.     hashNode hashtable[HashSize];
  17. public:
  18.     HashTable(){
  19.         for(int i=0 ; i<HashSize ; ++i){
  20.             hashtable[i].count=0;
  21.             hashtable[i].key=NULL;
  22.         }
  23.     }

  24.     ~HashTable(){
  25.         for(int i=0 ; i<HashSize ; ++i){
  26.             delete hashtable[i].key;
  27.         }
  28.     }

  29.     int HashIndex(int key,char *value){
  30.         int index= key % HashSize;
  31.         if(hashtable[index].count!=0){
  32.             if(strcmp(hashtable[index].key,value)==0)
  33.                 return index;
  34.             else
  35.                 return HashIndex(index+1,value);
  36.         }
  37.         return index;
  38.     }

  39.     int HashValue(char *value){
  40.         int h=0;
  41.         for(int i=0 ; value[i]!='\0' ; ++i){
  42.             h=31*h+value[i];
  43.         }

  44.         return HashIndex(h,value);
  45.     }

  46.     void HashInsert(char *value){
  47.         int index=HashValue(value);
  48.         //cout<<value<<":"<<index<<endl;
  49.         if(hashtable[index].key==NULL){
  50.             hashtable[index].key=new char[strlen(value)+1];
  51.             strncpy(hashtable[index].key,value,strlen(value));
  52.             hashtable[index].key[strlen(value)]='\0';

  53.             hashtable[index].count=1;
  54.             return;
  55.         }else if(strcmp(hashtable[index].key,value)==0){
  56.             hashtable[index].count++;
  57.             return;
  58.         }
  59.         
  60.     }

  61.     void output(){
  62.         ofstream out;
  63.         out.open("C:\\Users\\Thinkpad\\Desktop\\wordresult.txt");
  64.         for(int i=0 ; i<HashSize ; ++i){
  65.             if(hashtable[i].key){
  66.                 out<<hashtable[i].key<<":"<<hashtable[i].count<<endl;
  67.             }
  68.         }
  69.         out.close();
  70.     }
  71. };

  72. int _tmain(int argc, _TCHAR* argv[])
  73. {
  74.     HashTable ht;
  75.     char str[20];
  76.     ifstream in;
  77.     in.open("C:\\Users\\Thinkpad\\Desktop\\wordlist.txt");
  78.     if(in.is_open()){
  79.         while(!in.eof()){
  80.             in>>str;
  81.             ht.HashInsert(str);
  82.         }
  83.     }else{
  84.         cout<<"open file failed!"<<endl;
  85.     }

  86.     ht.output();
  87.     in.close();
  88.     return 0;
  89. }
    嗯,先从简单写起吧,之后写复杂的。
阅读(2520) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~