C++ 實現(xiàn)哈希表的實例
C++ 實現(xiàn)哈希表的實例
該散列表的散列函數(shù)采用了除法散列函數(shù)、乘法散列函數(shù)、全域散列函數(shù),每一個槽都是使用有序單向鏈表實現(xiàn)。
實現(xiàn)代碼:
LinkNode.h
#include<iostream> 
using namespace std; 
class Link; 
class LinkNode 
{ 
private: 
  int key; 
  LinkNode* next; 
  friend Link; 
public: 
  LinkNode():key(-1),next(NULL){} 
  LinkNode(int num):key(num),next(NULL){} 
  int Getkey() 
  { 
    return key; 
  } 
 
}; 
 Link.h
#include"LinkNode.h" 
class Hash; 
class Link 
{ 
private: 
  friend Hash; 
  LinkNode* head; 
  int length; 
public: 
  Link():head(NULL),length(0) 
  {} 
  Link(LinkNode* node):head(node) 
  { 
    length+=1; 
  } 
  ~Link() 
  { 
    MakeEmpty(); 
  } 
  void MakeEmpty() 
  { 
    if(head==NULL) 
      return ; 
    LinkNode* p=head; 
    while(p) 
    { 
      head=head->next; 
      delete p; 
      p=head; 
    } 
  } 
  int GetLength() 
  { 
    return length; 
  } 
  void Insert(int num) 
  { 
    length++; 
    LinkNode* p=new LinkNode(num); 
    if(head==NULL) 
    { 
      head=p; 
      return ; 
    } 
    LinkNode* q=head,*t=head->next; 
    if(q->key>num) 
    { 
      head=p; 
      head->next=q; 
      return ; 
    } 
    while(t) 
    { 
      if(t->key>=num) 
      { 
        q->next=p; 
        p->next=t; 
        return ; 
      } 
      else 
      { 
        q=t; 
        t=t->next; 
      } 
    } 
    q->next=p; 
  } 
  bool Delete(int num) 
  { 
    if(head==NULL) 
    { 
      cout<<"the link is empty!"<<endl; 
      return 0; 
    } 
    LinkNode* p=head,*t=head->next; 
    if(p->key==num) 
    { 
      head=head->next; 
      delete p; 
      length--; 
      return 1; 
    } 
    while(t) 
    { 
      if(t->key==num) 
      { 
        p->next=t->next; 
        delete t; 
        length--; 
        return 1; 
      } 
      else if(t->key<num) 
      { 
        p=t; 
        t=t->next; 
      } 
    } 
    return 0; 
  } 
  int Search(int num) 
  { 
    LinkNode* p=head; 
    while(p) 
    { 
      if(p->key==num) 
      { 
        return num; 
      } 
      else if(p->key<num) 
      { 
        p=p->next; 
      } 
      else 
      { 
        return 0; 
      } 
    } 
    return 0; 
  } 
  bool IsEmpty() 
  { 
    if(head==NULL) 
    { 
      return 1; 
    } 
    else 
      return 0; 
  } 
  void Print(int num) 
  { 
    if(head==NULL) 
    { 
      cout<<"the"<<num<<"th link is null!"<<endl; 
    } 
    LinkNode* p=head; 
    while(p) 
    { 
      cout<<p->key<<" "; 
      p=p->next; 
    } 
    cout<<endl; 
  } 
}; 
 Hash.h
Hash表中每一個元素存儲一個鏈表
#include"Link.h" 
class Hash 
{ 
private: 
  Link*Table; 
public: 
  Hash(int num):Table(new Link [num]){} 
  ~Hash() 
  { 
    delete [] Table; 
  } 
  //除法散列法 
  int H1(int num,int m) 
  { 
    return num%m; 
  } 
  //乘法散列法 
  int H2(int num,float A,int m) 
  { 
    float fnum=(float)num; 
    float re=((fnum*A)-(int)(fnum*A))*m; 
    return (int)re; 
  } 
  //全域散列 
  int H3(int num,int p,int m) 
  { 
    int a,b; 
    a=rand()%p; 
    b=rand()%p; 
    return ((a*num+b)%p)%m; 
  } 
  void Insert(int num,int n) 
  { 
    int key; 
     
    if(n==1) 
    { 
      key=H1(num,17); 
    } 
    else if(n==2) 
    { 
      key=H2(num,0.618033,17); 
    } 
    else 
    { 
      key=H3(num,701,17); 
    } 
    Table[key].Insert(num); 
  } 
  bool Delete(int num,int n) 
  { 
    int key;   
    if(n==1) 
    { 
      key=H1(num,17); 
    } 
    else if(n==2) 
    { 
      key=H2(num,0.618033,17); 
    } 
    else 
    { 
      key=H3(num,701,17); 
    } 
    return Table[key].Delete(num); 
  } 
  int Search(int num,int n) 
  { 
    int key; 
     
    if(n==1) 
    { 
      key=H1(num,17); 
    } 
    else if(n==2) 
    { 
      key=H2(num,0.618033,17); 
    } 
    else 
    { 
      key=H3(num,701,17); 
    } 
      if(Table[key].Search(num)!=0) 
      { 
        return key+1; 
      } 
      else 
        return -1; 
  } 
  void Print(int num) 
  { 
    int i; 
    for(i=0;i<num;i++) 
    { 
      if(Table[i].IsEmpty()) 
        continue; 
      Table[i].Print(i); 
    } 
  } 
}; 
 main.h
#include"Hash.h" 
int main() 
{ 
  Hash hash(1000),ha(100),sh(100); 
  int a[15]={15,6,9,4,7,32,569,419,78,125,635,46,456,16,457}; 
  int i; 
  for(i=0;i<15;i++) 
  { 
    hash.Insert(a[i],1); 
  } 
   
  for(i=0;i<15;i++) 
  { 
    ha.Insert(a[i],2); 
  } 
  cout<<endl; 
  for(i=0;i<15;i++) 
  { 
    sh.Insert(a[i],3); 
  } 
  hash.Print(1000); 
  cout<<endl; 
  ha.Print(100); 
  cout<<endl; 
  sh.Print(100); 
  cout<<endl; 
  cout<<hash.Search(46,1)<<endl; 
  if(hash.Delete(125,1)) 
  { 
    cout<<hash.Search(125,1)<<endl; 
  } 
} 
以上就是C++實現(xiàn)哈希表的實例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
下一篇:C++中stack、queue、vector的用法詳解
本文標題:C++ 實現(xiàn)哈希表的實例
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1211.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
 - 01-10數(shù)據(jù)結構課程設計-用棧實現(xiàn)表達式求值的方法詳解
 - 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
 - 01-10深入理解C++中常見的關鍵字含義
 - 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
 - 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
 - 01-10使用C++實現(xiàn)全排列算法的方法詳解
 - 01-10c++中inline的用法分析
 - 01-10用C++實現(xiàn)DBSCAN聚類算法
 - 01-10深入全排列算法及其實現(xiàn)方法
 


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
 
隨機閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 04-02jquery與jsp,用jquery
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10C#中split用法實例總結
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-10delphi制作wav文件的方法
 - 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 


