-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpract_1(Hash).cpp
More file actions
132 lines (130 loc) · 1.85 KB
/
Copy pathpract_1(Hash).cpp
File metadata and controls
132 lines (130 loc) · 1.85 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
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
/*PRACTICAL:-1
LINEAR PROBING
*/
#include<iostream>
using namespace std;
class hash1
{
public:
long long int hash[10];
hash1()
{
for(int i=0;i<10;i++)
{
hash[i]=0;
}
}
void insert();
void display();
void del();
void search();
};
void hash1::insert()
{
long long int x;
int index;
cout<<"Enter mobile number to insert: ";
cin>>x;
index=x % 10;
if(hash[index]==0)
{
hash[index]=x;
}
else
{
for(int i=index+1;i<10;i++)
{
if(hash[i]==0)
{
hash[i]=x;
break;
}
else if(i==9)
{
i=0;
}
else
i++;
}
}
}
void hash1::display()
{
cout<<"---------------HASH TABLE-------------------"<<endl;
for(int i=0;i<10;i++)
{
cout<<i<<" "<<hash[i]<<endl;
}
}
void hash1::del()
{
long long int x;
cout<<"Enter mobile number to delete: ";
cin>>x;
int flag=0;
for(int i=0;i<10;i++)
{
if(hash[i]==x)
{
hash[i]=0;
flag=1;
}
}
if(flag==0)
{
cout<<"Mobile number not found!";
}
}
void hash1::search()
{
long long int x;
cout<<"Enter mobile number to search: ";
cin>>x;
int flag=0;
for(int i=0;i<10;i++)
{
if(hash[i]==x)
{
cout<<"Key Found";
flag=1;
}
}
if(flag==0)
{
cout<<"Mobile number not found!";
}
}
int main()
{
hash1 h;
int ch;
cout<<"----------------MENU-------------------"<<endl;
cout<<"1)Insert\n2)Display\n3)Delete\n4)Search\n5)Exit"<<endl;
cout<<"----------------------------------------"<<endl;
do
{
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1:
h.insert();
break;
case 2:
h.display();
break;
case 3:
h.del();
break;
case 4:
h.search();
break;
case 5:
cout<<"Exit";
break;
default:
cout<<"Invalid choice!!"<<endl;
}
}while(ch!=5);
return 0;
}