-
Notifications
You must be signed in to change notification settings - Fork 4
/
chainhashing.cpp
54 lines (51 loc) · 926 Bytes
/
chainhashing.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
#include<iostream>
#define m 10
#define size 20
using namespace std;
struct hashing
{
int data;
struct hashing *next;
};
/*main function*/
int main()
{
struct hashing *hash[m],*newnode,*temp;
int list[size]={23,54,10,55,67,32,44,77,2,9,53,94,30,15,167,352,434,767,27,98},i,pos;
/*set position empty*/
for(i=0;i<m;++i)hash[i]=NULL;
for(i=0;i<size;++i)
{
pos = (2*list[i]+3)%m;
if(hash[pos]==NULL)
{
newnode = new hashing;
newnode->data=list[i];
newnode->next=NULL;
hash[pos]=newnode;
}
else
{
temp=hash[pos];
while(temp->next!=NULL)
{
temp=temp->next;
}
newnode=new hashing;
newnode->data=list[i];
newnode->next=NULL;
temp->next=newnode;
}
}
for(i=0;i<m;++i)
{
temp=hash[i];
cout<<"hash index : ->"<<i;
while(temp!=NULL)
{
cout<<"\t"<<temp->data;
temp=temp->next;
}
cout<<"\n";
}
}