diff --git a/Hashing/Chaining.cpp b/Hashing/Chaining.cpp new file mode 100644 index 000000000..6242312cf --- /dev/null +++ b/Hashing/Chaining.cpp @@ -0,0 +1,132 @@ +#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]==NULL){ + head[h] = temp; + curr=head[h]; + } + else{ + curr=head[h]; + while(curr->next!=NULL) + curr=curr->next; + curr->next = temp; + } +} + +void display(int mod){ + struct node *temp; + int i; + for(i=0;inext; + } + cout<data; + cout<data==x) + break; + else if(temp->next==NULL) + break; + temp=temp->next; + } + if(temp->next!=NULL) + cout<<"Element found"; + else{ + if(temp->data==x) + cout<<"Element found"; + else + cout<<"Element not found"; + } +} + +int main(void){ + init(); + int c,x,mod; + cout<<"Enter the size of Hash Table. = "; + cin>>mod; + while(1){ + cout<>c; + switch(c){ + case 1:{ + cout<<"Enter element to add = "; + cin>>x; + int h = hash(x,mod); + if(h<0) + h=fabs(h); + add(x,h); + break; + } + case 2:{ + cout<<"Enter element to search = "; + cin>>x; + int h=hash(x,mod); + find(x,h); + break; + } + case 3:{ + cout<<"Enter element to generate hash = "; + cin>>x; + cout<<"Hash of "<