From 37a2be33b72fc6cc16bb8d4f15fe6025f21979ee Mon Sep 17 00:00:00 2001 From: vasutomar Date: Wed, 18 Apr 2018 18:06:51 +0530 Subject: [PATCH] Hashing with collision resolution --- Hashing/Chaining.cpp | 94 ++++++++++++++++++++------------------------ 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/Hashing/Chaining.cpp b/Hashing/Chaining.cpp index 6242312cf..c518e2f6f 100644 --- a/Hashing/Chaining.cpp +++ b/Hashing/Chaining.cpp @@ -1,43 +1,45 @@ +// #include #include using namespace std; -struct node{ + +struct Node { int data; - struct node *next; + struct Node *next; } *head[100],*curr; -void init(){ +void init() { for(int i=0;i<100;i++) head[i]=NULL; } -void add(int x,int h){ - struct node *temp = new node; +void add(int x,int h) { + struct Node *temp = new Node; temp->data = x; temp->next = NULL; - if(head[h]==NULL){ + if(!head[h]) { head[h] = temp; - curr=head[h]; + curr = head[h]; } - else{ + else { curr=head[h]; - while(curr->next!=NULL) - curr=curr->next; + while(curr->next) + curr = curr->next; curr->next = temp; } } -void display(int mod){ - struct node *temp; +void display(int mod) { + struct Node *temp; int i; - for(i=0;inext; } @@ -47,39 +49,35 @@ void display(int mod){ } } -int hash(int x,int mod){ +int hash(int x,int mod) { return x%mod; } -void find(int x,int h){ - struct node *temp =head[h]; - if(head[h]==NULL){ +void find(int x,int h) { + struct Node *temp =head[h]; + if(!head[h]) { cout<<"Element not found"; return; } - while(1){ - if(temp->data==x) - break; - else if(temp->next==NULL) - break; + while(temp->data !=x && temp->next) temp=temp->next; - } - if(temp->next!=NULL) + if(temp->next) cout<<"Element found"; else{ - if(temp->data==x) + if(temp->data == x) cout<<"Element found"; else - cout<<"Element not found"; + cout<< "Element not found"; } } -int main(void){ +int main(void) { init(); - int c,x,mod; + int c,x,mod,h; cout<<"Enter the size of Hash Table. = "; cin>>mod; - while(1){ + bool loop = true; + while(loop) { cout<>c; - switch(c){ - case 1:{ + switch(c) { + case 1: cout<<"Enter element to add = "; cin>>x; - int h = hash(x,mod); - if(h<0) - h=fabs(h); + h = hash(x,mod); + h = fabs(h); add(x,h); break; - } - case 2:{ + case 2: cout<<"Enter element to search = "; cin>>x; - int h=hash(x,mod); + h = hash(x,mod); find(x,h); break; - } - case 3:{ + case 3: cout<<"Enter element to generate hash = "; cin>>x; cout<<"Hash of "<