Skip to content

Commit c050d9a

Browse files
Add files via upload
1 parent 2191c7f commit c050d9a

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

hashing.cpp

+46-52
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,30 @@ struct node
77
int data;
88
struct node *next;
99
};
10-
struct node *start[size];
10+
struct node *start[size]={NULL};
1111

1212

1313

14-
int table[size];int hash,i;
14+
int table[size]={0};int hash,i;
1515

1616

17-
void linear_probing(int rem,int elem)
17+
void linear_probing(int hash,int elem)
1818
{
19-
int temp,flag=0;
20-
temp=rem;
21-
while(flag==1)
19+
int flag=0;
20+
while(flag!=1)
2221
{
23-
if(rem==size-1)
22+
if(hash==size-1)
23+
hash=-1;
24+
hash++;
25+
if(table[hash]==0)
2426
{
25-
for(i=0;i<=temp;i++)
26-
{
27-
if(table[i]==0)
28-
{
29-
table[i]=elem;
30-
flag==1;
31-
}
32-
else
33-
{
34-
continue;
35-
}
36-
}
37-
}
38-
else
39-
{
40-
while(rem!=size-1 && flag==0)
41-
{
42-
if(table[rem]==0)
43-
{
44-
45-
table[rem]=elem;
46-
flag=1;
47-
}
48-
49-
else
50-
{
51-
rem++;
52-
}
53-
}
27+
table[hash]=elem;
28+
flag=1;
5429
}
30+
5531
}
32+
33+
5634

5735
}
5836
struct node *separate_chaining(struct node *ptr,int num)
@@ -74,7 +52,6 @@ struct node *separate_chaining(struct node *ptr,int num)
7452

7553
}
7654
return ptr;
77-
7855
}
7956

8057
void display()
@@ -96,7 +73,7 @@ void display()
9673
}
9774

9875

99-
void hashing(int keyValue)
76+
void hashing1(int keyValue)
10077
{
10178

10279
hash=(keyValue)%(size);
@@ -106,20 +83,28 @@ void hashing(int keyValue)
10683
return;
10784
}
10885
else
109-
start[hash]=separate_chaining(start[hash],keyValue);
86+
linear_probing(hash,keyValue);
11087

11188

11289
}
113-
114-
int main()
90+
void hashing2(int keyValue)
11591
{
116-
for(i=0;i<size;i++)
92+
93+
hash=(keyValue)%(size);
94+
if(table[hash]==0)
11795
{
118-
table[i]=0;
119-
start[i]=NULL;
96+
table[hash]=keyValue;
97+
return;
12098
}
99+
else
100+
start[hash]=separate_chaining(start[hash],keyValue);
101+
121102

122-
int numbers[10],n;
103+
}
104+
105+
int main()
106+
{
107+
int numbers[10],n,opt;
123108
printf("\n Enter the number of eleemnts (less than or equal to %d ):",size);
124109
scanf("%d",&n);
125110
if(n>size)
@@ -132,13 +117,22 @@ int main()
132117
printf("\n Enter the elements:");
133118
for(i=0;i<n;i++)
134119
scanf("\t %d",&numbers[i]);
135-
for(i=0;i<n;i++)
136-
hashing(numbers[i]);
137-
display();
120+
printf("1.Linear Probing\n2.Seperate Chaining\nEnter your option:");
121+
scanf("%d",&opt);
122+
switch(opt)
123+
{
124+
case 1:
125+
for(i=0;i<n;i++)
126+
hashing1(numbers[i]);
127+
display();break;
128+
case 2:
129+
for(i=0;i<n;i++)
130+
hashing2(numbers[i]);
131+
display();break;
132+
default:
133+
printf("\nPlease enter crt option");
134+
}
135+
138136
}
139137
}
140-
141-
142-
143-
144138

0 commit comments

Comments
 (0)