diff --git a/Hashing/Chaining.cpp b/Hashing/Chaining.cpp new file mode 100644 index 000000000..09350f4de --- /dev/null +++ b/Hashing/Chaining.cpp @@ -0,0 +1,123 @@ +#include +#include +using namespace std; + +struct Node { + int data; + struct Node *next; +} *head[100],*curr; + +void init() { + for(int i=0;i<100;i++) + head[i]=NULL; +} + +void add(int x,int h) { + struct Node *temp = new Node; + temp->data = x; + temp->next = NULL; + if(!head[h]) { + head[h] = temp; + curr = head[h]; + } + else { + curr=head[h]; + while(curr->next) + curr = curr->next; + curr->next = temp; + } +} + +void display(int mod) { + struct Node *temp; + int i; + for(i=0;inext; + } + cout<data; + cout<data !=x && temp->next) + temp=temp->next; + if(temp->next) + cout<<"Element found"; + else{ + if(temp->data == x) + cout<<"Element found"; + else + cout<< "Element not found"; + } +} + +int main(void) { + init(); + int c,x,mod,h; + cout<<"Enter the size of Hash Table. = "; + cin>>mod; + bool loop = true; + while(loop) { + cout<>c; + switch(c) { + case 1: + cout<<"Enter element to add = "; + cin>>x; + h = hash(x,mod); + h = fabs(h); + add(x,h); + break; + case 2: + cout<<"Enter element to search = "; + cin>>x; + h = hash(x,mod); + find(x,h); + break; + case 3: + cout<<"Enter element to generate hash = "; + cin>>x; + cout<<"Hash of "<