-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhash3.cpp
More file actions
99 lines (81 loc) · 1.71 KB
/
hash3.cpp
File metadata and controls
99 lines (81 loc) · 1.71 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
89
90
91
92
93
94
95
96
97
#include<iostream>
#include<cstring>
using namespace std;
const int tablesize=10;
struct person{
char name[30];
char phonenumber[15];
bool isfull=false;
};
int hashfunction(const char*phone){
int hash=0;
for(int i=0;phone[i]!='\0';i++)
hash=(hash*10+(phone[i]-'0'))%tablesize;
return hash;
}
void insert(person hashtable[],const char*phone,const char*name){
int comparison=0;
int index=hashfunction(phone),starting=index;
while(hashtable[index].isfull){
comparison++;
index=(index+1)%tablesize;
if(index==starting){
cout<<"memory full:";
return ;
}
}
strcpy (hashtable[index].phonenumber,phone);
strcpy (hashtable[index].name,name);
hashtable[index].isfull=true;
cout<<"inserted at index"<<index<<" "<<phone<<"->"<<name;
}
void searching(person hashtable[],const char*phone){
int comparison=0;
int index=hashfunction(phone),starting=index;
while(hashtable[index].isfull){
comparison++;
if(strcmp(hashtable[index].phonenumber,phone)==0){
cout<<"found at index:"<<index<<" "<<phone<<"->"<<hashtable[index].name<<"comparison is:"<<comparison;
return;
}
index=(index+1)%tablesize;
if(index==starting)
break;
}
cout<<"phone number is not found:";
}
int main(){
person hashtable[tablesize];
int choice;
char name[30];
char phone[15];
do{
cout<<"1-insert a data:"<<endl;
cout<<"2-search a data:"<<endl;
cout<<"enter a your choice:"<<endl;
cin>>choice;
switch(choice){
case 1:
int n;
cout<<"enter a number of person:";
cin>>n;
for(int i=0;i<n;i++){
cout<<"enter a phone number:";
cin>>phone;
cin.ignore();
cout<<"enter a name of person:";
cin.getline(name,30);
insert(hashtable,phone,name);
}
break;
case 2:
cout<<"enter a phone number:";
cin>>phone;
searching(hashtable,phone);
break;
default:
cout<<"end:";
}
}while(choice!=3);
return 0;
}