设为首页 加入收藏

TOP

C++哈希表
2015-07-22 20:10:13 来源: 作者: 【 】 浏览:5
Tags:哈希
#include 
   
     #include 
    
      #define MAX 13 using namespace std; template
     
       struct Node { Type *data; Node *next; Node(const Type *str = ) :data(new Type[strlen(str) + 1]), next(NULL) { strcpy(data,str); } }; template
      
        struct Mnode { Node
       
         *adj; int count;//计数。 Mnode() :adj(NULL), count(0){} }; template
        
          class Hash { public: Hash(){} long hash(const char *str) { const char *p = str; long nhash=0; while (*p) nhash = (nhash << 5) + nhash + *p++; return nhash; //Times33算法。 } void Insert(const char *str) { Node
         
           *s = new Node
          
           (str); long key = hash(str)%MAX; Node
           
             *p = node[key].adj; Node
            
              *pr = NULL; while (p != NULL) { pr = p; p = p->next; } if (pr == NULL) { node[key].adj = s; } else { s->next = p; pr->next = s; } node[key].count++; } void Printf() { int i = 0; Node
             
               *p = NULL; for (; i < MAX; i++) { if (node[i].count>0) { p = node[i].adj; while (p != NULL) { cout << p->data << ; p = p->next; } cout << endl; } } } private: Mnode
              
                node[MAX]; }; int main() { Hash
               
                 sh; sh.Insert(liuuiyan); sh.Insert(benleng); sh.Insert(huang); sh.Printf(); return 0; }
               
              
             
            
           
          
         
        
       
      
     
    
   

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdoj-1086-You can Solve a Geome.. 下一篇Majority Number

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: