-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.cpp
More file actions
131 lines (114 loc) · 2.41 KB
/
7.cpp
File metadata and controls
131 lines (114 loc) · 2.41 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
/*
Implement all the functions of a dictionary (ADT) using hashing.
Data: Set of (key, value) pairs, Keys are mapped to values, Keys must be comparable, Keys must be unique Standard Operations:
Insert(key, value), Find(key), Delete(key)
*/
#include<iostream>
#define M 10
using namespace std;
class hash
{
public:
int a[M];
hash();
void insert();
void search();
void Delete();
};
hash::hash()
{
for(int i=0;i<M;i++)
a[i]=-1;
}
void hash::insert()
{
int key,index;
cout<<"\nEnter key value : ";
cin>>key;
index=key%M;
if(a[index]==-1)
a[index]=key;
else
{
while(a[index]!=-1)
{
if(index==M-1)
index=-1;
index++;
index=index%M;
}
a[index]=key;
}
}
void hash::search()
{
int key,index,flag=1;
cout<<"\nEnter key value : ";
cin>>key;
index=key%M;
if(a[index]==key)
cout<<"\nElement found..........";
else
{
for(int i=index;i<M+index && a[i]!=-1;i++,i=i%M)
{
if(a[i]==key)
{
cout<<"\nElement found...........";
flag=0;
break;
}
}
}
if(flag!=0)
cout<<"\nElement not found !!!!!!";
}
void hash::Delete()
{
int key,index,flag=0;
cout<<"\nEnter key value : ";
cin>>key;
index=key%M;
if(a[index]==key)
{
a[index]=-1;
cout<<"\nElement Deleted successfully ....";
}
else
{
for(int i=index;i<M+index && a[i]!=-1;i++,i=i%M)
{
if(a[i]==key)
{
a[index]=-1;
cout<<"\nElement Deleted successfully ....";
flag=1
break;
}
}
}
if(flag!=1)
cout<<"\nElement not found !!!!!!";
}
int main()
{
hash o;
int ch=0;
do
{
cout<<"\n---------MENU----------\n1. Insert\n2. Search\n3. Delete\n4. Exit\nEnter choice : ";
cin>>ch;
switch(ch)
{
case 1:
o.insert();
break;
case 2:
o.search();
break;
case 3:
o.Delete();
break;
}
}while(ch!=4);
}