C++模板类设计哈希表 程序

/******************************************************************************

*****作者:李文嘉  如有转帖,请表出处

*****CHash.h

*****

*****

*******************************************************************************/

#ifndef __HASHCACHE__H
#define __HASHCACHE__H

#include<iostream>
#include<string>

using namespace std;

 class CHashTableFull
 {
  public:
   CHashTableFull(string theMessage="The hash table is full")
   {
    message =theMessage;
   }
   void outputMessage(){cout<<message<<endl;}
  private:
   string message;
 };
 
 template<class K,class E> class CHashTable;
 
 template<class K,class E>
 class CHashNode
 {
  private:
   CHashNode(){}
   CHashNode(const K &Host,const K&Path,const E Port):host(Host),path(Path),port(Port),Next(0){}
   K host;
   K path;
   E port;
   CHashNode<K,E> *Next;
  public:
    friend class CHashTable<K,E>;
    //void print(){cout<<host<<endl<<path<<endl<<port<<endl;}
 };
 
 //没有设置复制构造函数,未来可以添加
 template<class K,class E>
 class CHashTable
 {
  public:
   CHashTable(int Size=1024);
   ~CHashTable(){delete [] CHashTables;}
   
   bool empty()const{return HashSize==0;}
   int size()const{return HashSize;}
   //int IdleSize()const{return HashSize-FullSize;}
   int HashId(K urls)
   {
    unsigned long hashValue=0;
    int length=(int)urls.length();
    for(int i=0;i<length;i++)
     hashValue=5*hashValue+urls.at(i);
    
    return int(hashValue);
   }
   void Insert(const K Urls,const K Host,const K Path,const E Port); /*MD5*/
   void OutPut(ostream&out)const;
  protected:
   //int search(const K&)const;
  protected:
   CHashNode<K,E>**CHashTables;
   int HashSize;
   //int FullSize;
 };
 
template<class K, class E>
CHashTable<K,E>::CHashTable(int Size)
{
  HashSize=Size;
  //FullSize=0;
 
  CHashTables=new CHashNode<K,E>*[HashSize]; //暂不加入new异常处理
}

template<class K,class E> 
void CHashTable<K,E>::Insert(const K Urls,const K Host,const K Path,const E Port)//不能随便使用引用,否则会发生难以预想的问题
{
 int id=(int)HashId(Urls)%HashSize;
 
 //CHashNode<K,E> *p=CHashTables[id];
 //CHashNode<K,E> *node=new CHashNode<K,E>(Host,Path,Port);
 //cout<<p<<endl<<CHashTables[id]<<endl;
 
 if(CHashTables[id])
  {
   CHashNode<K,E> *p=CHashTables[id];
   CHashNode<K,E> *node=new CHashNode<K,E>(Host,Path,Port);
    node->Next=p->Next;
    p->Next=node;
    
    return;
  }
  else
  {
   CHashTables[id]=new CHashNode<K,E>(Host,Path,Port);
   cout<<id<<endl<<endl<<CHashTables[id]<<endl;
   //cout<<Host<<endl<<Path<<endl;
   //p.print();
   //p=node;
   
   return;
  }

template<class K,class E> 
void CHashTable<K,E>::OutPut(ostream&out)const
{
 for(int i=0;i<HashSize;i++)
 {
  CHashNode<K,E> *p=CHashTables[i];
  while(p)
  {
   out<<"111"<<endl;
   out<<p->host<<":"<<p->path<<":"<<p->port<<":"<<endl;
   p=p->Next;
  }
 }
}

template <class K,class E>
ostream& operator<<(ostream& out, const CHashTable<K,E> &x)
{
 x.OutPut(out);
 return out;
}


#endif

 

/***********************************************************************

*****

*****

***********************************************************************/

#include"Chash.h"

#include<iostream>
#include<string>

using namespace std;
//using namespace crawler;

int main()
{
 CHashTable<string,int>z;
 
 z.Insert("www.baidu.com","www.baidu.com","192.124,343,3434",80);
 z.Insert("www.google.com","www.google.com","192.124,343,3434",80);
 z.Insert("www.baidu.com","www.baidu.com/index.html","192.124,343,3434",80);
 
 cout<<z<<endl;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值