-
Notifications
You must be signed in to change notification settings - Fork 0
/
adtdict.cpp
180 lines (180 loc) · 3.97 KB
/
adtdict.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
int chain;
string word;
string mean;
} obj[10];
int hash_func(string wd)
{
int key = 0;
for (int i = 0; i < wd.size(); i++)
{
key = wd[i];
}
return key % 10;
}
int collision(int key, string wd, string mn)
{
int i = 1;
while ((key % 10) < 10)
{
if (obj[(key + i) % 10].word == "-")
{
obj[(key + i) % 10].word = wd;
obj[(key + i) % 10].mean = mn;
break;
}
else
{
i++;
}
}
return (key + i) % 10;
}
void hash_ini()
{
for (int i = 0; i < 10; i++)
{
obj[i].word = "-";
obj[i].mean = "-";
obj[i].chain = -1;
}
}
void hash_table()
{
string wd, mn;
cout << "Enter a word: ";
cin >> wd;
cout << "\nENter the meaning for word : ";
cin >> mn;
cout << endl;
int hash_key = hash_func(wd);
if (obj[hash_key].word == "-")
{
obj[hash_key].word = wd;
obj[hash_key].mean = mn;
}
else
{
obj[hash_key].chain = collision(hash_key, wd, mn);
}
}
void display()
{
cout << "index"
<< "\t"
<< "word"
<< "\t"
<< "meaning"
<< "\t"
<< "chain" << endl;
for (int i = 0; i < 10; i++)
{
cout << i << "\t" << obj[i].word << "\t" << obj[i].mean << "\t" << obj[i].chain << endl;
}
}
void del_key(string word)
{
int key = hash_func(word);
if (obj[key].word == word and obj[key].chain != -1)
{
int ch = obj[key].chain;
obj[key].word = obj[ch].word;
obj[key].mean = obj[ch].mean;
obj[key].chain = -1;
obj[ch].word = "-";
obj[ch].mean = "-";
obj[ch].chain = -1;
}
else if (obj[key].word == word and (obj[key].chain == -1))
{
obj[key].word = "-";
obj[key].mean = "-";
obj[key].chain = -1;
}
else if (obj[key].word != word and (obj[key].chain != -1))
{
int ch = obj[key].chain;
obj[ch].word = "-";
obj[ch].mean = "-";
obj[key].chain = -1;
}
else
{
cout << "word not found" << endl;
}
}
void find(string word)
{
int key = hash_func(word);
if (obj[key].word == word)
{
cout << "Found the Word" << endl;
cout << obj[key].word << "--> " << obj[key].mean << endl;
}
else if (obj[key].chain != -1)
{
int k = obj[key].chain;
cout << "Found the word" << endl;
cout << obj[k].word << "--> " << obj[k].mean << endl;
}
else
{
cout << "Not Found" << endl;
}
}
int main()
{
int ch;
string del_wd;
string src;
hash_ini();
do
{
cout << "============OPERATIONS==========" << endl;
cout << "\n1.Insert data in hash table" << endl;
cout << "2.Display Hash Table" << endl;
cout << "3.Delete word from Hash Table" << endl;
cout << "4.Find word in a hash table" << endl;
cout << "5.exit" << endl;
cout << "\n\nEnter your choice: " << endl;
cin >> ch;
cout << endl;
switch (ch)
{
case 1:
cout << "Enter no of entries that you want to make : " << endl;
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
hash_table();
}
break;
case 2:
display();
break;
case 3:
cout << "Enter the word that you want to delete: " << endl;
cin >> del_wd;
del_key(del_wd);
break;
case 4:
cout << "Enter the word you want to search: " << endl;
cin >> src;
find(src);
break;
case 5:
cout << "End!!!";
break;
default:
cout << "Invalid Choice, Pls try again!" << endl;
}
} while (ch < 5);
cout << "Done Succesfully" << endl;
return 0;
}