-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashset.c
More file actions
274 lines (231 loc) · 6.46 KB
/
Copy pathhashset.c
File metadata and controls
274 lines (231 loc) · 6.46 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* ============================================================================
* HashSet Implementation in C
* ============================================================================
* Description: This program implements a HashSet data structure that stores
* unique integer values using a hash table with separate chaining.
*
* Operations:
* - create_hashset: Initialize a new hash set
* - add: Add an element to the hash set
* - contains: Check if an element exists in the hash set
* - remove_element: Remove an element from the hash set
* - get_size: Get the number of elements in the hash set
* - display: Display all elements in the hash set
* - clear_hashset: Remove all elements from the hash set
* - destroy: Free all allocated memory
*
* Compilation: gcc -o hashset_c hashset.c
* Execution: ./hashset_c
* ============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define DEFAULT_CAPACITY 16
/* Node structure for separate chaining */
typedef struct Node {
int value;
struct Node* next;
} Node;
/* HashSet structure */
typedef struct HashSet {
Node** buckets;
int capacity;
int size;
} HashSet;
/* Function prototypes */
HashSet* create_hashset(int capacity);
int hash_function(int value, int capacity);
bool add(HashSet* set, int value);
bool contains(HashSet* set, int value);
bool remove_element(HashSet* set, int value);
int get_size(HashSet* set);
void display(HashSet* set);
void clear_hashset(HashSet* set);
void destroy(HashSet* set);
/*
* create_hashset: Create and initialize a new HashSet
* Input: capacity (number of buckets)
* Output: Pointer to HashSet structure, or NULL on failure
*/
HashSet* create_hashset(int capacity) {
HashSet* set = (HashSet*)malloc(sizeof(HashSet));
if (!set) {
return NULL;
}
set->buckets = (Node**)calloc(capacity, sizeof(Node*));
if (!set->buckets) {
free(set);
return NULL;
}
set->capacity = capacity;
set->size = 0;
return set;
}
/*
* hash_function: Calculate hash value for a given key
* Input: value to hash, capacity
* Output: hash value (bucket index)
*/
int hash_function(int value, int capacity) {
return abs(value) % capacity;
}
/*
* add: Add an element to the HashSet
* Input: HashSet pointer, value to add
* Output: true if added, false if already exists or error
*/
bool add(HashSet* set, int value) {
if (!set) return false;
/* Check if value already exists */
if (contains(set, value)) {
return false;
}
/* Calculate hash */
int index = hash_function(value, set->capacity);
/* Allocate new node */
Node* new_node = (Node*)malloc(sizeof(Node));
if (!new_node) {
return false;
}
new_node->value = value;
new_node->next = set->buckets[index];
set->buckets[index] = new_node;
set->size++;
return true;
}
/*
* contains: Check if an element exists in the HashSet
* Input: HashSet pointer, value to check
* Output: true if exists, false if not
*/
bool contains(HashSet* set, int value) {
if (!set) return false;
int index = hash_function(value, set->capacity);
Node* current = set->buckets[index];
while (current) {
if (current->value == value) {
return true;
}
current = current->next;
}
return false;
}
/*
* remove_element: Remove an element from the HashSet
* Input: HashSet pointer, value to remove
* Output: true if removed, false if not found
*/
bool remove_element(HashSet* set, int value) {
if (!set) return false;
int index = hash_function(value, set->capacity);
Node* current = set->buckets[index];
Node* prev = NULL;
while (current) {
if (current->value == value) {
if (prev) {
prev->next = current->next;
} else {
set->buckets[index] = current->next;
}
free(current);
set->size--;
return true;
}
prev = current;
current = current->next;
}
return false;
}
/*
* get_size: Get the number of elements in the HashSet
* Input: HashSet pointer
* Output: size
*/
int get_size(HashSet* set) {
return set ? set->size : 0;
}
/*
* display: Display all elements in the HashSet
* Input: HashSet pointer
*/
void display(HashSet* set) {
if (!set) return;
if (set->size == 0) {
printf("HashSet is empty\n");
return;
}
for (int i = 0; i < set->capacity; i++) {
Node* current = set->buckets[i];
if (current) {
printf("%d: ", i);
while (current) {
printf("%d ", current->value);
current = current->next;
}
printf("\n");
}
}
}
/*
* clear_hashset: Remove all elements from the HashSet
* Input: HashSet pointer
*/
void clear_hashset(HashSet* set) {
if (!set) return;
for (int i = 0; i < set->capacity; i++) {
Node* current = set->buckets[i];
while (current) {
Node* temp = current;
current = current->next;
free(temp);
}
set->buckets[i] = NULL;
}
set->size = 0;
}
/*
* destroy: Free all memory used by the HashSet
* Input: HashSet pointer
*/
void destroy(HashSet* set) {
if (!set) return;
clear_hashset(set);
free(set->buckets);
free(set);
}
/*
* Main function - Test the HashSet implementation
*/
int main() {
/* Create HashSet */
HashSet* set = create_hashset(DEFAULT_CAPACITY);
if (!set) {
fprintf(stderr, "Failed to create HashSet\n");
return 1;
}
/* Add elements */
add(set, 10);
add(set, 20);
add(set, 15);
add(set, 25);
add(set, 10); /* Duplicate, should not be added */
/* Display HashSet */
printf("HashSet after adding elements:\n");
display(set);
/* Check if elements exist */
printf("\nContains 15: %s\n", contains(set, 15) ? "Yes" : "No");
printf("Contains 100: %s\n", contains(set, 100) ? "Yes" : "No");
/* Print size */
printf("\nSize: %d\n", get_size(set));
/* Remove element */
remove_element(set, 20);
/* Display after removal */
printf("\nHashSet after removing 20:\n");
display(set);
printf("\nSize: %d\n", get_size(set));
/* Clean up */
destroy(set);
return 0;
}