@@ -7,52 +7,30 @@ struct node
7
7
int data;
8
8
struct node *next;
9
9
};
10
- struct node *start[size];
10
+ struct node *start[size]={ NULL } ;
11
11
12
12
13
13
14
- int table[size];int hash,i;
14
+ int table[size]={ 0 } ;int hash,i;
15
15
16
16
17
- void linear_probing (int rem ,int elem)
17
+ void linear_probing (int hash ,int elem)
18
18
{
19
- int temp,flag=0 ;
20
- temp=rem;
21
- while (flag==1 )
19
+ int flag=0 ;
20
+ while (flag!=1 )
22
21
{
23
- if (rem==size-1 )
22
+ if (hash==size-1 )
23
+ hash=-1 ;
24
+ hash++;
25
+ if (table[hash]==0 )
24
26
{
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 ;
54
29
}
30
+
55
31
}
32
+
33
+
56
34
57
35
}
58
36
struct node *separate_chaining (struct node *ptr,int num)
@@ -74,7 +52,6 @@ struct node *separate_chaining(struct node *ptr,int num)
74
52
75
53
}
76
54
return ptr;
77
-
78
55
}
79
56
80
57
void display ()
@@ -96,7 +73,7 @@ void display()
96
73
}
97
74
98
75
99
- void hashing (int keyValue)
76
+ void hashing1 (int keyValue)
100
77
{
101
78
102
79
hash=(keyValue)%(size);
@@ -106,20 +83,28 @@ void hashing(int keyValue)
106
83
return ;
107
84
}
108
85
else
109
- start[hash]= separate_chaining (start[ hash] ,keyValue);
86
+ linear_probing ( hash,keyValue);
110
87
111
88
112
89
}
113
-
114
- int main ()
90
+ void hashing2 (int keyValue)
115
91
{
116
- for (i=0 ;i<size;i++)
92
+
93
+ hash=(keyValue)%(size);
94
+ if (table[hash]==0 )
117
95
{
118
- table[i]= 0 ;
119
- start[i]= NULL ;
96
+ table[hash]=keyValue ;
97
+ return ;
120
98
}
99
+ else
100
+ start[hash]=separate_chaining (start[hash],keyValue);
101
+
121
102
122
- int numbers[10 ],n;
103
+ }
104
+
105
+ int main ()
106
+ {
107
+ int numbers[10 ],n,opt;
123
108
printf (" \n Enter the number of eleemnts (less than or equal to %d ):" ,size);
124
109
scanf (" %d" ,&n);
125
110
if (n>size)
@@ -132,13 +117,22 @@ int main()
132
117
printf (" \n Enter the elements:" );
133
118
for (i=0 ;i<n;i++)
134
119
scanf (" \t %d" ,&numbers[i]);
135
- for (i=0 ;i<n;i++)
136
- hashing (numbers[i]);
137
- display ();
120
+ printf (" 1.Linear Probing\n 2.Seperate Chaining\n Enter 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 (" \n Please enter crt option" );
134
+ }
135
+
138
136
}
139
137
}
140
-
141
-
142
-
143
-
144
138
0 commit comments