Skip to content

Commit 37a2be3

Browse files
committed
Hashing with collision resolution
1 parent 7747d3a commit 37a2be3

File tree

1 file changed

+43
-51
lines changed

1 file changed

+43
-51
lines changed

Hashing/Chaining.cpp

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,45 @@
1+
//
12
#include<iostream>
23
#include<math.h>
34
using namespace std;
4-
struct node{
5+
6+
struct Node {
57
int data;
6-
struct node *next;
8+
struct Node *next;
79
} *head[100],*curr;
810

9-
void init(){
11+
void init() {
1012
for(int i=0;i<100;i++)
1113
head[i]=NULL;
1214
}
1315

14-
void add(int x,int h){
15-
struct node *temp = new node;
16+
void add(int x,int h) {
17+
struct Node *temp = new Node;
1618
temp->data = x;
1719
temp->next = NULL;
18-
if(head[h]==NULL){
20+
if(!head[h]) {
1921
head[h] = temp;
20-
curr=head[h];
22+
curr = head[h];
2123
}
22-
else{
24+
else {
2325
curr=head[h];
24-
while(curr->next!=NULL)
25-
curr=curr->next;
26+
while(curr->next)
27+
curr = curr->next;
2628
curr->next = temp;
2729
}
2830
}
2931

30-
void display(int mod){
31-
struct node *temp;
32+
void display(int mod) {
33+
struct Node *temp;
3234
int i;
33-
for(i=0;i<mod;i++){
34-
if(head[i]==NULL){
35-
cout<<"Key = "<<i<<" is empty"<<endl;
35+
for(i=0;i<mod;i++) {
36+
if(!head[i]) {
37+
cout<<"Key "<<i<<" is empty"<<endl;
3638
}
37-
else{
38-
cout<<"Key = "<<i<<" Value = ";
39+
else {
40+
cout<<"Key "<<i<<" has values = ";
3941
temp = head[i];
40-
while(temp->next!=NULL){
42+
while(temp->next) {
4143
cout<<temp->data<<" ";
4244
temp=temp->next;
4345
}
@@ -47,39 +49,35 @@ void display(int mod){
4749
}
4850
}
4951

50-
int hash(int x,int mod){
52+
int hash(int x,int mod) {
5153
return x%mod;
5254
}
5355

54-
void find(int x,int h){
55-
struct node *temp =head[h];
56-
if(head[h]==NULL){
56+
void find(int x,int h) {
57+
struct Node *temp =head[h];
58+
if(!head[h]) {
5759
cout<<"Element not found";
5860
return;
5961
}
60-
while(1){
61-
if(temp->data==x)
62-
break;
63-
else if(temp->next==NULL)
64-
break;
62+
while(temp->data !=x && temp->next)
6563
temp=temp->next;
66-
}
67-
if(temp->next!=NULL)
64+
if(temp->next)
6865
cout<<"Element found";
6966
else{
70-
if(temp->data==x)
67+
if(temp->data == x)
7168
cout<<"Element found";
7269
else
73-
cout<<"Element not found";
70+
cout<< "Element not found";
7471
}
7572
}
7673

77-
int main(void){
74+
int main(void) {
7875
init();
79-
int c,x,mod;
76+
int c,x,mod,h;
8077
cout<<"Enter the size of Hash Table. = ";
8178
cin>>mod;
82-
while(1){
79+
bool loop = true;
80+
while(loop) {
8381
cout<<endl;
8482
cout<<"PLEASE CHOOSE -"<<endl;
8583
cout<<"1. Add element."<<endl;
@@ -88,38 +86,32 @@ int main(void){
8886
cout<<"4. Display Hash table."<<endl;
8987
cout<<"5. Exit."<<endl;
9088
cin>>c;
91-
switch(c){
92-
case 1:{
89+
switch(c) {
90+
case 1:
9391
cout<<"Enter element to add = ";
9492
cin>>x;
95-
int h = hash(x,mod);
96-
if(h<0)
97-
h=fabs(h);
93+
h = hash(x,mod);
94+
h = fabs(h);
9895
add(x,h);
9996
break;
100-
}
101-
case 2:{
97+
case 2:
10298
cout<<"Enter element to search = ";
10399
cin>>x;
104-
int h=hash(x,mod);
100+
h = hash(x,mod);
105101
find(x,h);
106102
break;
107-
}
108-
case 3:{
103+
case 3:
109104
cout<<"Enter element to generate hash = ";
110105
cin>>x;
111106
cout<<"Hash of "<<x<<" is = "<<hash(x,mod);
112107
break;
113-
}
114-
case 4:{
108+
case 4:
115109
display(mod);
116-
}
117-
default:{
118110
break;
119-
}
111+
default:
112+
loop = false;
113+
break;
120114
}
121-
if(c>4)
122-
break;
123115
cout<<endl;
124116
}
125117
/*add(1,&head1);

0 commit comments

Comments
 (0)