-
Notifications
You must be signed in to change notification settings - Fork 0
/
dslab_12.c
70 lines (67 loc) · 1.26 KB
/
dslab_12.c
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
#include<stdlib.h>
int n,m,*ht,count=0,i,flag=0,hi;
void createHT();
void insertHT(int key);
void displayHT();
void main()
{
int key;
printf("Enter value of n\n");
scanf("%d",&n);
printf("Enter 2 digit memory location\n");
scanf("%d",&m);
createHT();
for(i=0;i<n;i++)
{
printf("enter 4 digit key word\n");
scanf("%d",&key);
if(count==m){
printf("hash table is full\n");
break;
}
else{
insertHT(key);
}
}
displayHT();
}
void createHT()
{
ht=(int*)malloc(m*sizeof(int));
if(m==0){
printf("Memory not allocated\n");
}
else{
for(i=0;i<m;i++){
ht[i]=-1;
}
}
}
void insertHT(int key)
{
hi=key%m;
while(ht[hi]!=-1){
hi=(hi+1)%m;
flag=1;
}
if(flag==1){
printf("collusion detected and resolved using linear probing\n");
flag=0;
}
ht[hi]=key;
count++;
}
void displayHT()
{
int i;
if(count==0){
printf("hashtable is empty\n");
exit(0);
}
else{
for(i=0;i<m;i++){
printf("[%d]->%d\n",i,ht[i]);
}
}
}