-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds.h
401 lines (362 loc) · 10.2 KB
/
ds.h
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* MIT License
Copyright (c) 2024 Abenezer L.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef DS_H
#define DS_H
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
// =======================================
// HybridArray Implementation
// =======================================
typedef struct {
int *data;
size_t size;
size_t capacity;
} HybridArray;
/**
* @brief Initializes a HybridArray structure.
*
* @param array Pointer to the HybridArray to initialize.
*/
void hybrid_array_init(HybridArray *array) {
array->data = NULL;
array->size = 0;
array->capacity = 0;
}
/**
* @brief Adds a value to the end of the HybridArray.
*
* @param array Pointer to the HybridArray.
* @param value The integer value to add to the array.
*/
void hybrid_array_push_back(HybridArray *array, int value) {
if (array->capacity == 0) {
array->capacity = 10;
array->data = malloc(array->capacity * sizeof(int));
} else if (array->size == array->capacity) {
array->capacity *= 2;
array->data = realloc(array->data, array->capacity * sizeof(int));
}
array->data[array->size++] = value;
}
/**
* @brief Retrieves the value at a specific index in the HybridArray.
*
* @param array Pointer to the HybridArray.
* @param index The index of the value to retrieve.
* @return The integer value at the specified index.
* @note Exits the program if the index is out of bounds.
*/
int hybrid_array_get(const HybridArray *array, size_t index) {
if (index >= array->size) {
fprintf(stderr, "Index out of bounds: %zu\n", index);
exit(EXIT_FAILURE);
}
return array->data[index];
}
/**
* @brief Frees the memory used by the HybridArray.
*
* @param array Pointer to the HybridArray to destroy.
*/
void hybrid_array_destroy(HybridArray *array) {
free(array->data);
array->data = NULL;
array->size = 0;
array->capacity = 0;
}
/**
* @brief Prints the contents of the HybridArray.
*
* @param array Pointer to the HybridArray.
*/
void hybrid_array_print(const HybridArray *array) {
for (size_t i = 0; i < array->size; i++) {
printf("%d ", array->data[i]);
}
printf("\n");
}
// =======================================
// Hashmap Implementation
// =======================================
typedef struct HashmapEntry {
char *key;
void *value;
struct HashmapEntry *next;
} HashmapEntry;
typedef struct {
HashmapEntry **buckets;
size_t size;
size_t count;
} Hashmap;
static unsigned long hash(const char *str) {
unsigned long hash = 5381;
int c;
while ((c = *str++)) {
hash = ((hash << 5) + hash) + c;
}
return hash;
}
/**
* @brief Creates a new hashmap.
*
* @param size The number of buckets in the hashmap.
* @return Pointer to the newly created Hashmap, or NULL if memory allocation fails.
*/
Hashmap *hashmap_create(size_t size) {
Hashmap *map = malloc(sizeof(Hashmap));
if (!map) return NULL;
map->buckets = calloc(size, sizeof(HashmapEntry *));
if (!map->buckets) {
free(map);
return NULL;
}
map->size = size;
map->count = 0;
return map;
}
/**
* @brief Frees the memory used by the hashmap.
*
* @param map Pointer to the Hashmap to destroy.
*/
void hashmap_destroy(Hashmap *map) {
for (size_t i = 0; i < map->size; i++) {
HashmapEntry *entry = map->buckets[i];
while (entry) {
HashmapEntry *temp = entry;
entry = entry->next;
free(temp->key);
free(temp);
}
}
free(map->buckets);
free(map);
}
/**
* @brief Inserts a key-value pair into the hashmap.
*
* @param map Pointer to the Hashmap.
* @param key The string key for the entry.
* @param value Pointer to the value associated with the key.
* @return True if insertion was successful, false otherwise.
*/
bool hashmap_insert(Hashmap *map, const char *key, void *value) {
unsigned long hash_value = hash(key) % map->size;
HashmapEntry *new_entry = malloc(sizeof(HashmapEntry));
if (!new_entry) return false;
new_entry->key = strdup(key);
if (!new_entry->key) {
free(new_entry);
return false;
}
new_entry->value = value;
new_entry->next = map->buckets[hash_value];
map->buckets[hash_value] = new_entry;
map->count++;
return true;
}
/**
* @brief Retrieves a value associated with a key in the hashmap.
*
* @param map Pointer to the Hashmap.
* @param key The string key to look up.
* @return Pointer to the value associated with the key, or NULL if the key is not found.
*/
void *hashmap_get(Hashmap *map, const char *key) {
unsigned long hash_value = hash(key) % map->size;
HashmapEntry *entry = map->buckets[hash_value];
while (entry) {
if (strcmp(entry->key, key) == 0) {
return entry->value;
}
entry = entry->next;
}
return NULL;
}
/**
* @brief Removes a key-value pair from the hashmap.
*
* @param map Pointer to the Hashmap.
* @param key The string key to remove.
* @return True if the key was successfully removed, false if the key was not found.
*/
bool hashmap_remove(Hashmap *map, const char *key) {
unsigned long hash_value = hash(key) % map->size;
HashmapEntry *entry = map->buckets[hash_value];
HashmapEntry *prev = NULL;
while (entry) {
if (strcmp(entry->key, key) == 0) {
if (prev) {
prev->next = entry->next;
} else {
map->buckets[hash_value] = entry->next;
}
free(entry->key);
free(entry);
map->count--;
return true;
}
prev = entry;
entry = entry->next;
}
return false;
}
/**
* @brief Prints the contents of the hashmap.
*
* @param map Pointer to the Hashmap.
*/
void hashmap_print(Hashmap *map) {
printf("Hashmap contents:\n");
for (size_t i = 0; i < map->size; i++) {
HashmapEntry *entry = map->buckets[i];
if (entry) {
printf("Bucket %zu: ", i);
while (entry) {
printf("(%s -> %p) ", entry->key, entry->value);
entry = entry->next;
}
printf("\n");
}
}
}
// =======================================
// Linked List Implementation
// =======================================
typedef struct Node {
int data;
struct Node* next;
} Node;
/**
* @brief Creates a new node with the given data.
*
* @param data The integer data for the new node.
* @return Pointer to the newly created node, or NULL if memory allocation fails.
*/
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (!newNode) {
printf("Memory allocation failed\n");
return NULL;
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}
/**
* @brief Inserts a new node with the given data at the head of the list.
*
* @param head Pointer to the pointer to the head of the list.
* @param data The integer data to insert.
*/
void insertAtHead(Node** head, int data) {
Node* newNode = createNode(data);
if (!newNode) return;
newNode->next = *head;
*head = newNode;
}
/**
* @brief Inserts a new node with the given data at the tail of the list.
*
* @param head Pointer to the pointer to the head of the list.
* @param data The integer data to insert.
*/
void insertAtTail(Node** head, int data) {
Node* newNode = createNode(data);
if (!newNode) return;
if (*head == NULL) {
*head = newNode;
return;
}
Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
/**
* @brief Deletes the first node with the given data value.
*
* @param head Pointer to the pointer to the head of the list.
* @param data The integer value to delete from the list.
*/
void deleteNode(Node** head, int data) {
if (*head == NULL) return;
Node* temp = *head;
if (temp->data == data) {
*head = temp->next;
free(temp);
return;
}
Node* prev = NULL;
while (temp != NULL && temp->data != data) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
/**
* @brief Searches for a node with the specified data.
*
* @param head Pointer to the head of the list.
* @param data The integer value to search for.
* @return Pointer to the node containing the data, or NULL if not found.
*/
Node* search(Node* head, int data) {
Node* temp = head;
while (temp != NULL) {
if (temp->data == data) {
return temp;
}
temp = temp->next;
}
return NULL;
}
/**
* @brief Prints the contents of the linked list.
*
* @param head Pointer to the head of the list.
*/
void printList(Node* head) {
Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
/**
* @brief Frees the memory allocated for the linked list.
*
* @param head Pointer to the head of the list.
*/
void freeList(Node* head) {
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
#endif