-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashing_new.c
157 lines (138 loc) · 2.75 KB
/
hashing_new.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<stdio.h>
#include<stdlib.h>
typedef struct myhash
{
int *table;
int bucket;
}myhash;
void create(myhash *h,int b)
{
h->bucket=b;
h->table=(int *)malloc(sizeof(int)*h->bucket);
for(int i=0;i<h->bucket;i++)
{
h->table[i]=-1;
}
}
void insert(myhash *h,int key)
{
int j=key%h->bucket;
int i=j;
while(h->table[i]!=-1 && h->table[i]!=-2)
{
if(h->table[i]==key)
{
printf("Error while inserting\n");
printf("Note: Element in the hash table should be unique\n");
return;
}
i=(i+1)%h->bucket;
if(i==j)
{
printf("No empty slots in the hash table\n");
return;
}
}
printf("Insertion Successfull\n");
h->table[i]=key;
}
void deletee(myhash *h,int key)
{
int j=key%h->bucket;
int i=j;
while(h->table[i]!=-1)
{
if(h->table[i]==key)
{
h->table[i]=-2;
printf("deletion successfull\n");
return;
}
i=(i+1)%h->bucket;
if(i==j)
{
printf("Deletion failed\n");
printf("No empty slots\n");
return;
}
}
printf("deletiom failed\n");
printf("Element not found\n");
}
void search(myhash *h,int key)
{
int j=key%h->bucket;
int i=j;
while(h->table[i]!=-1)
{
if(h->table[i]==key)
{
printf("searching successfull\n");
printf("Note :element found\n");
return;
}
i=(i+1)%h->bucket;
if(i==j)
{
printf("searching failed\n");
printf("No empty slots\n");
return;
}
}
printf("searching failed\n");
printf("Element not found\n");
}
void display(myhash *h)
{
for(int i=0;i<h->bucket;i++)
{
printf("|%d|",h->table[i]);
}
printf("\n");
}
int main()
{
int b;
printf("Enter the capacity of hash table\n");
scanf("%d",&b);
myhash h;
int n;
int ele;
int opt;
create(&h,b);
do
{
printf("Enter the option\n");
printf("1.insert\t2.delete\t3.search\t0.exit\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("Enter number of elements to be inserted\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("Enter element\n");
scanf("%d",&ele);
insert(&h,ele);
}
break;
case 2:
printf("Enter the key to be deleted\n");
scanf("%d",&ele);
deletee(&h,ele);
break;
case 3:
printf("Enter the key to be searched\n");
scanf("%d",&ele);
search(&h,ele);
break;
case 4:
display(&h);
break;
case 0:
break;
}
}while(opt!=0);
return 0;
}