哈希表的构造和逆输出

用c++实现了哈希表的构造,采用的链地址法,处理冲突的方法是最简单的线性探测,最后利用优先队列priority_queue结合拓扑排序实现哈希表的逆输出
txt文件

10 1 13 12 21 33 34 38 27 22 32

头文件 Hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include<iostream>

class Hash
{
private:
typedef struct Cell
{
int m_Element;
}Cell;
int m_TableSize;
Cell* TheCell;
public:
Hash(int tablesize);
int Find(int Key);
int FindPos(int Key);
void Insert(int Key);
void ShowHash() const;
void Reshow();
~Hash();
};
实现文件 Hash.cpp
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
#include<iostream>
#include"Hash.h"
#include<list>
#include<vector>
#include<stack>
#include<queue>
using namespace std;

Hash::Hash(int tablesize)
{
if ((tablesize - 3) / 4 < 1)
{
while (tablesize != 7)
{
tablesize++;
}
}
else if ((tablesize - 3) % 4 != 0)
{
while ((tablesize - 3) % 4 != 0)
{
tablesize++;
}
}

this->m_TableSize = tablesize;
this->TheCell = new Cell[this->m_TableSize];
for (int i = 0; i < this->m_TableSize; i++)
{
this->TheCell[i].m_Element = -1;
}
}

int Hash::Find(int Key)
{
int i = 1;
int pos = Key % this->m_TableSize;
int element = this->TheCell[pos].m_Element;
while (element != -1 && i <= this->m_TableSize)
{
pos = (pos+1) % this->m_TableSize;
i++;
element = this->TheCell[pos].m_Element;
}
if (i > this->m_TableSize)
{
cout << "没有空位了" << endl;
return -1;
}
return pos;
}

int Hash::FindPos(int Key)
{
for (int i = 0; i < this->m_TableSize; i++)
{
if (this->TheCell[i].m_Element == Key)
{
return i;
}
}
}

void Hash::Insert(int Key)
{

int pos = Find(Key);
if (pos == -1)
{
return;
}
this->TheCell[pos].m_Element = Key;
}

void Hash::ShowHash() const
{
for (int i = 0; i < this->m_TableSize; i++)
{
cout << this->TheCell[i].m_Element << " ";
}
}

void Hash::Reshow()
{
int temp;
int tempele;
int* degree = new int[this->m_TableSize];
priority_queue<int, vector<int>, greater<int> > q;
for (int i = 0; i < this->m_TableSize; i++)
{
degree[i] = 0;
}
vector<list<int>> l(this->m_TableSize);

for (int i = 0; i < this->m_TableSize; i++)
{
if (this->TheCell[i].m_Element > 0)
{
int probe = this->TheCell[i].m_Element % this->m_TableSize;
degree[i] = (this->m_TableSize + i - probe) % this->m_TableSize;
if (degree[i])
{
for (int j = 0; j < degree[i]; j++)
{
l[(probe + j) % this->m_TableSize].push_back(i);
}
}
else
q.push(this->TheCell[i].m_Element);
}
}

int mpos;
while (!q.empty())
{
tempele = q.top();
q.pop();
cout << tempele << " ";
mpos = this->FindPos(tempele);
while (!l[mpos].empty())
{
if (--degree[l[mpos].front()] == 0)
{
q.push(this->TheCell[l[mpos].front()].m_Element);
}
l[mpos].pop_front();
}
}
delete[] degree;
}

Hash::~Hash()
{
delete[] this->TheCell;
}
main文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include"Hash.h"
#include<fstream>
using namespace std;


int main()
{
ifstream ifs("yao.txt");
int num;
int key;
ifs >> num;
Hash h(num);
for (int i = 0; i < num; i++)
{
ifs >> key;
h.Insert(key);
}

h.Reshow();
ifs.close();
system("pause");
return 0;
}
结果: