• 哈希表类


    C++ code

      1 #include "stdafx.h"
      2 #pragma once
      3 #define IDType int
      4 
      5 //哈希函数:除留余数法。冲突处理:链地址法
      6 template <class T>
      7 class CDict
      8 {
      9 public:
     10     CDict(int storeL)
     11     {
     12         if(storeL>0)
     13             storeLen=storeL;
     14         else
     15             storeLen=100;
     16         initProc();
     17     }
     18     CDict()
     19     {
     20         storeLen=100;
     21         initProc();
     22     }
     23     //增加
     24     bool Add(IDType ID,T data)
     25     {
     26         if(Find(ID))//保持主键唯一
     27             return 0;
     28         else{
     29         //新建节点
     30         HNode * tmp=new HNode;
     31         tmp->next=NULL;
     32         tmp->ID=ID;
     33         tmp->data=data;
     34         //获得链地址
     35         int pos=ID % storeLen;
     36         //检测冲突
     37         if(!ChainHash[pos]) 
     38             ChainHash[pos]=tmp;
     39         else{
     40             HNode * rear=ChainHash[pos];
     41             while(rear->next) rear=rear->next;
     42             rear->next=tmp;
     43         }
     44         return 1;
     45         }
     46     }
     47     //删除
     48     bool Delete(IDType ID)
     49     {
     50         //获得链地址
     51         int pos=ID % storeLen;
     52         //检测冲突
     53         if(ChainHash[pos]) 
     54         {
     55             HNode * rear=ChainHash[pos];
     56             if(rear->ID==ID)//在首地址就找到了数据
     57             {
     58                 ChainHash[pos]=rear->next;
     59                 delete rear;
     60             }
     61             else{
     62                 while(rear->next)
     63                 {
     64                     if(rear->next->ID==ID)
     65                     {
     66                         //勾连
     67                         HNode * tmp=rear->next;
     68                         rear->next=tmp->next;
     69                         delete tmp;
     70                         return 1;
     71                     }
     72                     rear=rear->next;
     73                 }
     74             }
     75         }
     76         return 0;
     77     }
     78     bool SetAt(IDType ID,T data)
     79     {
     80         int pos=ID % storeLen;
     81         //检测冲突
     82         if(ChainHash[pos]) 
     83         {
     84             HNode * rear=ChainHash[pos];
     85             while(rear)
     86             {
     87                 if(rear->ID==ID) 
     88                 {
     89                     rear->data=data;
     90                     return 1;
     91                 }
     92                 rear=rear->next;
     93             }
     94         }
     95         return 0;
     96     }
     97     T Find(int ID)
     98     {
     99         int pos=ID % storeLen;
    100         //检测冲突
    101         if(ChainHash[pos]) 
    102         {
    103             HNode * rear=ChainHash[pos];
    104             while(rear)
    105             {
    106                 if(rear->ID==ID) return rear->data;
    107                 rear=rear->next;
    108             }
    109         }
    110         return NULL;
    111     }
    112     void showStruct()
    113     {
    114         int i=0;
    115         for(i=0;i<storeLen;i++)
    116         {
    117             if(ChainHash[i]) 
    118             {
    119                 HNode * rear=ChainHash[i];
    120                 while(rear)
    121                 {
    122                     printf(" → (%d,%d)",rear->ID,rear->data);
    123                     rear=rear->next;
    124                 }
    125                 printf(" → ∧");
    126             }
    127             else printf("");
    128             printf("
    ");
    129         }
    130     }
    131 protected:
    132 private:
    133     int storeLen;
    134     //构造数据结构:存放冲突数据的链表
    135     typedef struct HNode{
    136         T data;//数据域
    137         struct HNode* next;//指针域
    138         IDType ID;//主键域
    139     }HNode;
    140     //构造链哈希表用于存放HNode的地址
    141     HNode** ChainHash;
    142     void initProc()
    143     {
    144         //构造链哈希表
    145         ChainHash=new HNode*[storeLen];//ChainHash[ num ]的数据类型是HNode*,用来指向存储节点。
    146         int i;
    147         for(i=0;i<storeLen;i++) ChainHash[i]=NULL;//初始化
    148     }
    149 };
  • 相关阅读:
    Two Sum
    Binary Tree Preorder Traversal *
    Rotate Array
    Repeated DNA Sequences
    Symmetric Tree
    Path Sum
    Python初学——多线程Threading
    Python初学——窗口视窗Tkinter
    pyinstaller打包多个py文件和去除cmd黑框
    python获取当前路径
  • 原文地址:https://www.cnblogs.com/TQCAI/p/7606771.html
Copyright © 2020-2023  润新知