服务器之家

服务器之家 > 正文

C++ 实现哈希表的实例

时间:2021-05-31 14:18     来源/作者:liuzhanchen1987

C++ 实现哈希表的实例

该散列表的散列函数采用了除法散列函数、乘法散列函数、全域散列函数,每一个槽都是使用有序单向链表实现。

实现代码:

LinkNode.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#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表中每一个元素存储一个链表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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++实现哈希表的实例,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

原文链接:http://blog.csdn.net/liuzhanchen1987/article/details/7789977

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部