-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashTable.c
175 lines (151 loc) · 3.38 KB
/
hashTable.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include<stdio.h>
#include<stdlib.h>
struct data
{
int key;
int value;
};
struct data *array;
int capacity = 10;
int size = 0;
/* this function gives a unique hash code to the given key */
int hashcode(int key)
{
return (key % capacity);
}
/* it returns prime number just greater than array capacity */
int get_prime(int n)
{
if (n % 2 == 0)
{
n++;
}
for (; !if_prime(n); n += 2);
return n;
}
/* to check if given input (i.e n) is prime or not */
int if_prime(int n)
{
int i;
if ( n == 1 || n == 0)
{
return 0;
}
for (i = 2; i < n; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
void init_array()
{
int i;
capacity = get_prime(capacity);
array = (struct data*) malloc(capacity * sizeof(struct data));
for (i = 0; i < capacity; i++)
{
array[i].key = 0;
array[i].value = 0;
}
}
/* to insert a key in the hash table */
void insert(int key)
{
int index = hashcode(key);
if (array[index].value == 0)
{
/* key not present, insert it */
array[index].key = key;
array[index].value = 1;
size++;
printf("\n Key (%d) has been inserted \n", key);
}
else if(array[index].key == key)
{
/* updating already existing key */
printf("\n The key (%d) already present, hence updating its value \n", key);
array[index].value += 1;
}
else
{
/* key cannot be insert as the index is already containing some other key */
printf("\n ELEMENT CANNOT BE INSERTED \n");
}
}
/* to remove a key from hash table */
void remove_element(int key)
{
int index = hashcode(key);
if(array[index].value == 0)
{
printf("\n This key does not exist \n");
}
else {
array[index].key = 0;
array[index].value = 0;
size--;
printf("\n Key (%d) has been removed \n", key);
}
}
/* to display all the elements of a hash table */
void display()
{
int i;
for (i = 0; i < capacity; i++)
{
if (array[i].value == 0)
{
printf("\n Array[%d] has no elements \n");
}
else
{
printf("\n array[%d] has elements -:\n key(%d) and value(%d) \t", i, array[i].key, array[i].value);
}
}
}
int size_of_hashtable()
{
return size;
}
void main()
{
int choice, key, value, n, c;
init_array();
do {
printf("\n Implementation of Hash Table in C \n\n");
printf("MENU-: \n1.Inserting item in the Hash Table"
"\n2.Removing item from the Hash Table"
"\n3.Check the size of Hash Table"
"\n4.Display a Hash Table"
"\n\n Please enter your choice -:");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Inserting element in Hash Table\n");
printf("Enter key -:\t");
scanf("%d", &key);
insert(key);
break;
case 2:
printf("Deleting in Hash Table \n Enter the key to delete-:");
scanf("%d", &key);
remove_element(key);
break;
case 3:
n = size_of_hashtable();
printf("Size of Hash Table is-:%d\n", n);
break;
case 4:
display();
break;
default:
printf("Wrong Input\n");
}
printf("\n Do you want to continue-:(press 1 for yes)\t");
scanf("%d", &c);
}while(c == 1);
}