-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashTable.cpp
155 lines (128 loc) · 3.95 KB
/
HashTable.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
#include <iostream>
using namespace std;
int num; // Global variable, number of Hash Buckets
/*---------------------------------------------------------------------------*/
class table{
public:
table *next;
int data;
};
/*---------------------------------------------------------------------------*/
void insert(table *head[], int data){
int i;
table *new_data = new table();
new_data->data = data;
new_data->next = NULL;
i = data % num; // Hash Function
table *n = head[i];
if (head[i] == NULL)
head[i] = new_data;
else{
while (n->next != NULL)
n = n->next;
n->next = new_data;
}
}
/*---------------------------------------------------------------------------*/
void del(table *head[], int data){
int p;
p = data % num; // Hash Function
table *n = head[p];
table *prev = new table();
if (head[p]->data == data){
if (head[p]->next == NULL){
head[p] = NULL;
}
else{
head[p] = head[p]->next;
}
}
else{
while (n->data != data && n->next != NULL){
prev = n;
n = n->next;
}
if (n->data == data){
prev->next = n->next;
delete n;
}
else
cout << data << " is not present in the Hash Table\n";
}
}
/*---------------------------------------------------------------------------*/
int search(table *head[], int data) {
int p;
p = data % num; // Hash Function
table *n = head[p];
while (n != NULL){
if (n->data == data)
return p;
else
n = n->next;
}
return 0;
}
/*---------------------------------------------------------------------------*/
void display(table *head[]){
table *n = new table;
for (int p = 0; p < num; p++){
cout << p << " : ";
if (head[p] == NULL){
cout << "Null";
}
else{
for (n = head[p]; n != NULL; n = n->next){
cout << n->data << " -> ";
}
cout << "Null";
}
cout << endl;
}
}
/*---------------------------------------------------------------------------*/
int main(){
cout << "Enter the Number of Hash Buckets : ";
cin >> num;
cout << endl;
table *head[num] = {NULL}; // initialize all array elements as NULL
int a, b, key, ch;
do{
cout << "1 —————> Insert \n";
cout << "2 —————> Delete \n";
cout << "3 —————> Display \n";
cout << "4 —————> Check if an Element is Present \n";
cout << "5 —————> Exit \n";
cout << "\nEnter your choice : ";
cin >> ch;
switch (ch){
case (1):
cout << "Enter the value to be Inserted to the Hash Table : ";
cin >> a;
insert(head, a);
break;
case (2):
cout << "Enter the value to be Deleted from the Hash Table : ";
cin >> b;
del(head, b);
break;
case (3):
cout << "\nHash Table : \n\n";
display(head);
cout << endl;
break;
case (4):
cout << "Enter the Element to be Checked : ";
cin >> key;
if (search(head, key))
cout << key << " is present in the Hash Table in bucket " << search(head, key) << endl;
else
cout << key << " is not present in the Hash Table\n";
break;
case (5):
break;
default:
cout << "Enter a value between 1 and 5";
}
}while (ch != 5);
}