-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable_LinearProb.cpp
103 lines (91 loc) · 1.75 KB
/
HashTable_LinearProb.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
#include <iostream>
using namespace std;
#define MAX 100
struct Node
{
int info;
};
typedef struct Node NODE;
int Hash(int x)
{
return x % MAX;
}
void CreateHashTable(NODE *HT[])
{
for (int i = 0; i < MAX; i++)
HT[i] = NULL;
}
NODE *CreateNode(int x)
{
NODE *p = new NODE;
if (p == NULL)
return NULL;
p->info = x;
return p;
}
void InsertNode(NODE *HT[], int x)
{
int index = Hash(x);
NODE *p = CreateNode(x);
while (HT[index] != NULL)
{
index = Hash(index + 1);
}
HT[index] = p;
}
NODE *SearchNode(NODE *HT[], int x)
{
int index = Hash(x);
while (HT[index]->info != x && HT[index]->info != NULL)
{
index = Hash(index + 1);
}
if (HT[index] != NULL)
return HT[index];
return NULL;
}
void DeleteNode(NODE *HT[], int x)
{
int index = Hash(x);
while (HT[index]->info != x && HT[index]->info != NULL)
{
index = Hash(index + 1);
}
if (HT[index]->info == x)
HT[index] = NULL;
}
void PrintHashTable(NODE *HT[])
{
for (int i = 0; i < MAX; i++)
{
if (HT[i] != NULL)
cout << i << ": " << HT[i]->info << endl;
else
cout << i << ": NULL" << endl;
}
cout << endl;
}
int main()
{
NODE *HT[MAX];
CreateHashTable(HT);
InsertNode(HT, 1);
InsertNode(HT, 2);
InsertNode(HT, 3);
InsertNode(HT, 4);
InsertNode(HT, 5);
InsertNode(HT, 6);
InsertNode(HT, 7);
InsertNode(HT, 8);
InsertNode(HT, 9);
InsertNode(HT, 10);
PrintHashTable(HT);
NODE *p = SearchNode(HT, 1);
if (p != NULL)
cout << "Found: " << p->info << endl;
else
cout << "Not Found! " << endl;
DeleteNode(HT, 1);
PrintHashTable(HT);
return 0;
}