-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathA2.cpp
196 lines (160 loc) · 3.75 KB
/
A2.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <cstring>
#define size 7
using namespace std;
class Hashtable {
public:
char key[size], value[size], k[size];
string h[size][2];
int count, ch;
void input();
void display();
void hashf(char[]);
void linearp(string key, int);
void search();
void Delete();
Hashtable() {
for (int i = 0; i < size; i++)
for (int j = 0; j < 2; j++)
h[i][j] = "---";
count = 0;
}
};
void Hashtable ::input() {
cout << "\nEnter the key: ";
cin >> key;
cout << "Enter the value: ";
cin >> value;
hashf(key);
}
void Hashtable ::hashf(char key[]) {
int sum = 0;
for (int i = 0; i < strlen(key); i++)
sum = sum + int(key[i]);
ch = sum % size;
linearp(key, ch);
}
void Hashtable ::linearp(string key, int ch) {
if (count == size)
cout << "Table is Full";
else {
while (h[ch][0] != "---" && count != size)
ch = ++ch % size;
h[ch][0] = key;
h[ch][1] = value;
count++;
}
}
void Hashtable ::display() {
cout << "\n\t[Key]\t[Value]\n";
for (int i = 0; i < size; i++) {
for (int j = 0; j < 2; j++)
cout << "\t" << h[i][j];
cout << "\n";
}
}
void Hashtable ::search() {
cout << "\nEnter the string to search: ";
cin >> k;
int sum = 0;
for (int i = 0; i < strlen(k); i++)
sum = sum + int(k[i]);
ch = sum % size;
if (count == size)
cout << "\nSearch is not found \n";
else {
while (h[ch][0] != k && count != size)
ch = ++ch % size;
count++;
if (h[ch][0] == k)
cout << "String '" << k << "' found at index " << ch << "\n";
}
}
void Hashtable ::Delete() {
cout << "\nEnter the string to delete: ";
cin >> k;
int sum = 0;
for (int i = 0; i < strlen(k); i++)
sum = sum + int(k[i]);
ch = sum % size;
if (count == size)
cout << "Search is not found \n";
else
while (h[ch][0] != k && count != size)
ch = ++ch % size;
h[ch][0] = "---";
h[ch][1] = "---";
cout << "String '" << k << "' deleted from index " << ch << "\n";
}
int main() {
int ch;
Hashtable h1;
do {
cout << "\nEnter choice\n1.input 2.display 3.search 4.Delete\nChoice [1/2/3/4] ";
cin >> ch;
switch (ch) {
case 1:
h1.input();
break;
case 2:
h1.display();
break;
case 3:
h1.search();
break;
case 4:
h1.Delete();
break;
}
} while (ch < 5);
return 0;
}
/*
Enter choice
1.input 2.display 3.search 4.Delete
Choice [1/2/3/4] 1
Enter the key: name
Enter the value: Pratik
[Key] [Value]
--- ---
--- ---
--- ---
--- ---
name Pratik
--- ---
--- ---
Enter choice
1.input 2.display 3.search 4.Delete
Choice [1/2/3/4] 1
Enter the key: roll
Enter the value: 56
[Key] [Value]
roll 56
--- ---
--- ---
--- ---
name Pratik
--- ---
--- ---
Enter choice
1.input 2.display 3.search 4.Delete
Choice [1/2/3/4] 3
Enter the string to search: roll
String 'roll' found at index 0
Enter choice
1.input 2.display 3.search 4.Delete
Choice [1/2/3/4] 4
Enter the string to delete: name
String 'name' deleted from index 4
Enter choice
1.input 2.display 3.search 4.Delete
Choice [1/2/3/4] 2
[Key] [Value]
roll 56
--- ---
--- ---
--- ---
--- ---
--- ---
--- ---
*/