-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionary.cpp
More file actions
88 lines (79 loc) · 1.92 KB
/
Copy pathdictionary.cpp
File metadata and controls
88 lines (79 loc) · 1.92 KB
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
#include <iostream>
#include <cstring>
#define max 10
using namespace std;
struct dict {
char name[20];
long int mn;
int chain;
dict() {
strcpy(name, "-");
mn = 0;
chain = -1;
}
};
class Hash {
dict arr[max];
public:
int hashFun(long int a) {
return (a % 10);
}
void Insert() {
dict S;
cout << "Enter name and mobile no" << endl;
cin >> S.name >> S.mn;
int index;
index = hashFun(S.mn);
int start = index;
if (arr[index].mn == 0) {
arr[index] = S;
}
else
{
int prev = index;
while (arr[index].mn != 0) {
index = (index + 1) % max;
if (index == start) {
cout << "Hash table full";
break;
}
if (arr[prev].chain != -1) {
prev = arr[prev].chain;
}
}
arr[index] = S;
arr[prev].chain = index;
}
}
void Display() {
for (int i = 0; i < max; i++) {
cout << i << " " << arr[i].name << " " << arr[i].mn << " " << arr[i].chain << endl;
}
}
};
int main() {
Hash h;
int choice;
do {
cout << "Please enter your choice!!" << endl;
cout << "1.insert 2.display" << endl;
cin >> choice;
switch (choice) {
case 1:
int n;
cout << "Enter how many values you want to insert" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
h.Insert();
}
break;
case 2:
h.Display();
break;
default:
cout<<"invalid choice!!";
break;
}
} while (choice != 0);
return 0;
}