-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHash Table.cpp
More file actions
144 lines (131 loc) · 2.79 KB
/
Hash Table.cpp
File metadata and controls
144 lines (131 loc) · 2.79 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
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<limits.h>
using namespace std;
void Insert(int arr[],int hfn,int size)
{
int element, pos, n=0;
cout<<"Enter key element to insert\n";
cin>>element;
pos = element%hfn;
while(arr[pos] != INT_MIN)
{
if(arr[pos]==INT_MAX)
break;
pos = (pos+1)%hfn;
n++;
if(n==size)
break;
}
if(n==size)
{
cout<<"Hash table was full of elements\nNo placce to insert this elements\n\n";
}
else
{
arr[pos] = element;
}
}
void Delete(int arr[],int hfn, int size)
{
int element, n=0,pos;
cout<<"Enter element to delete\n";
cin>>element;
pos = element%hfn;
while(n++ != size)
{
if(arr[pos]==INT_MIN)
{
cout<<"Element not found in hash table\n";
break;
}
else if(arr[pos]==element)
{
arr[pos] = INT_MAX;
cout<<"Element deleted\n";
break;
}
else
{
pos = (pos+1)%hfn;
}
}
if(--n == size)
{
cout<<"Element not found in hash table\n";
}
return;
}
void Search(int arr[],int hfn, int size)
{
int element, pos, n=0;
cout<<"Enter element to search\n";
cin>>element;
pos = element % hfn;
while(n++ != size)
{
if(arr[pos]==element)
{
cout<<"Element found at index "<<pos<<endl;
break;
}
else
{
if(arr[pos]==INT_MAX || arr[pos]!=INT_MIN)
{
pos = (pos+1)%hfn;
}
}
}
if(--n==size)
{
cout<<"Element not found in hash table\n";
}
return;
}
void display(int arr[],int size)
{
int i;
cout<<"Index\tvalue\n";
for(i=0;i<size;i++)
{
cout<<i<<"\t"<<arr[i]<<"\n";
}
return;
}
int main()
{
int size, hfn,i,choice;
cout<<"Enter size of hash table: \n";
cin>>size;
int arr[size];
cout<<"Enter hash function [if mod 10 enter 10]\n";
cin>>hfn;
for(i=0;i<size;i++)
{
arr[i] = INT_MIN;
}
do{
cout<<"Enter your choise\n";
cout<<"1-> Insert\n 2-> Delete\n 3-> Searching\n 4-> Display\n 0-> Exit\n";
cin>>choice;
switch(choice)
{
case 1:
Insert(arr,hfn,size);
break;
case 2:
Delete(arr,hfn,size);
break;
case 3:
Search(arr,hfn,size);
break;
case 4:
display(arr,size);
break;
default:
cout<<"Wrong choice\n";
break;
}
}while(choice);
return 0;
}