-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.txt
205 lines (184 loc) · 3.4 KB
/
08.txt
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
197
198
199
200
201
202
203
204
205
//Consider telephone book database of N clients. Make use of a hash table implementation to
quickly look up client‘s telephone number.
#include<iostream>
#include<string.h>
using namespace std;
class HashFunction
{
typedef struct hash
{
long key;
char name[10];
}hash;
hash h[10];
public:
HashFunction();
void insert();
void display();
int find(long);
void Delete(long);
};
HashFunction::HashFunction()
{
int i;
for(i=0;i<10;i++)
{
h[i].key=-1;
strcpy(h[i].name,"NULL");
}
}
void HashFunction::Delete(long k)
{
int index=find(k);
if(index==-1)
{
cout<<"\n\tKey Not Found";
}
else
{
h[index].key=-1;
strcpy(h[index].name,"NULL");
cout<<"\n\tKey is Deleted";
}
}
int HashFunction::find(long k)
{
int i;
for(i=0;i<10;i++)
{
if(h[i].key==k)
{
cout<<"\n\t"<<h[i].key<<" is Found at "<<i<<" Location With Name "<<h[i].name;
return i;
}
}
if(i==10)
{
return -1;
}
}
void HashFunction::display()
{
int i;
cout<<"\n\t\tKey\t\tName";
for(i=0;i<10;i++)
{
cout<<"\n\th["<<i<<"]\t"<<h[i].key<<"\t\t"<<h[i].name;
}
}
void HashFunction::insert()
{
char ans,n[10],ntemp[10];
long k,temp;
int v,hi,cnt=0,flag=0,i;
do
{
if(cnt>=10)
{
cout<<"\n\tHash Table is FULL";
break;
}
cout<<"\n\tEnter a Telephone No: ";
cin>>k;
cout<<"\n\tEnter a Client Name: ";
cin>>n;
hi=k%10;// hash function
if(h[hi].key==-1)
{
h[hi].key=k;
strcpy(h[hi].name,n);
}
else
{
if(h[hi].key%10!=hi)
{
temp=h[hi].key;
strcpy(ntemp,h[hi].name);
h[hi].key=k;
strcpy(h[hi].name,n);
for(i=hi+1;i<10;i++)
{
if(h[i].key==-1)
{
h[i].key=temp;
strcpy(h[i].name,ntemp);
flag=1;
break;
}
}
for(i=0;i<hi && flag==0;i++)
{
if(h[i].key==-1)
{
h[i].key=temp;
strcpy(h[i].name,ntemp);
break;
}
}
}
else
{
for(i=hi+1;i<10;i++)
{
if(h[i].key==-1)
{
h[i].key=k;
strcpy(h[i].name,n);
flag=1;
break;
}
}
for(i=0;i<hi && flag==0;i++)
{
if(h[i].key==-1)
{
h[i].key=k;
strcpy(h[i].name,n);
break;
}
}
}
}
flag=0;
cnt++;
cout<<"\n\t..... Do You Want to Insert More Key: ";
cin>>ans;
}while(ans=='y'||ans=='Y');
}
main()
{
long k;
int ch,index;
char ans;
HashFunction obj;
do
{
cout<<"\n\t***** Dictionary (ADT) *****";
cout<<"\n\t1. Insert\n\t2. Display\n\t3. Find\n\t4. Delete\n\t5. Exit";
cout<<"\n\t..... Enter Your Choice: ";
cin>>ch;
switch(ch)
{
case 1: obj.insert();
break;
case 2: obj.display();
break;
case 3: cout<<"\n\tEnter a Key Which You Want to Search: ";
cin>>k;
index=obj.find(k);
if(index==-1)
{
cout<<"\n\tKey Not Found";
}
break;
case 4: cout<<"\n\tEnter a Key Which You Want to Delete: ";
cin>>k;
obj.Delete(k);
break;
case 5:
break;
}
cout<<"\n\t..... Do You Want to Continue in Main Menu: ";
cin>>ans;
}while(ans=='y'||ans=='Y');
}