forked from adee-dev/clg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing.cpp
164 lines (151 loc) · 2.94 KB
/
hashing.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
#include <bits/stdc++.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 idx = find(k);
if(idx == -1){
cout << "\n\t key not found";
}
else{
h[idx].key = -1;
strcpy(h[idx].name, "NULL");
cout << "\n\t key 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;
}
return i;
}
void HashFunction :: display(){
int i;
cout << "\n\t\t key \t\t name ";
for(i= 0; i < 10; ++i){
cout << "\n\t h[ " << 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\t hash table is full ";
break;
}
cout << "\n\t Enter a telephone number : ";
cin >> k;
cout << "\n\t Enter a client name : ";
cin >> n;
hi = k % 10;
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++){
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 : y/n ";
cin >> ans;
}
while(ans == 'y' || ans == 'Y');
}
int main(){
long k;
int ch, idx;
char ans;
HashFunction obj;
do{
cout<<"\n\t**TELEPHONE(ADT)** ";
cout << "\n\t 1. insert \n\t 2.display \n\t 3.find \n\t 4.Delete \n\t 5. 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\t... Enter a key you want to seacrh ";
cin >> k ;
idx = obj.find(k);
if(idx == -1){
cout << "\n\t NOt FOund " ;
}
break;
case 4 : cout << "\n\t.... Enter a key Which you want to delete";
cin >> k;
obj.Delete(k);
break;
case 5 :
break;
}
cout << "\n\t Do you want to continue to main menu ? : y/n " ;
cin >> ans ;
}
while(ans == 'y' || ans == 'Y');
return 0;
}