-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashmap.c
151 lines (126 loc) · 4.11 KB
/
Hashmap.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
/*This is a C program that I made that uses a hash map to locate a single house in a city.
In this example, the houses are represented by their street addresses,
and the hash map is used to store the mapping between addresses and the corresponding houses. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define a structure to represent a house
typedef struct {
char address[100];
int houseNumber;
} House;
// Define a structure for the hash map node
typedef struct HashNode {
char key[100];
House value;
struct HashNode* next;
} HashNode;
// Define a structure for the hash map
typedef struct {
HashNode** buckets;
int size;
int count; // Number of elements in the hash map
} HashMap;
// Hash function to convert a string key to an index
int hashFunction(const char* key, int size) {
int hash = 0;
while (*key) {
hash = (hash * 31) + (*key++);
}
return hash % size;
}
// Initialize a new hash map
HashMap* initializeHashMap(int size) {
HashMap* map = (HashMap*)malloc(sizeof(HashMap));
map->size = size;
map->count = 0;
map->buckets = (HashNode**)calloc(size, sizeof(HashNode*));
return map;
}
// Resize the hash map to double its current size
void resizeHashMap(HashMap* map) {
int newSize = map->size * 2;
// Create a new hash map with the doubled size
HashMap* newMap = initializeHashMap(newSize);
// Rehash and insert all existing elements into the new map
for (int i = 0; i < map->size; i++) {
HashNode* current = map->buckets[i];
while (current != NULL) {
insertHouse(newMap, current->key, current->value.houseNumber);
current = current->next;
}
}
// Update the current map with the new map's data
free(map->buckets);
map->size = newMap->size;
map->count = newMap->count;
map->buckets = newMap->buckets;
// Free the memory allocated for the new map
free(newMap);
}
// Insert a house into the hash map
void insertHouse(HashMap* map, const char* address, int houseNumber) {
// Check if resizing is needed
if (map->count >= map->size) {
resizeHashMap(map);
}
int index = hashFunction(address, map->size);
// Create a new house node
HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
strcpy(newNode->key, address);
newNode->value.houseNumber = houseNumber;
strcpy(newNode->value.address, address);
newNode->next = NULL;
// Insert the node into the hash map
if (map->buckets[index] == NULL) {
map->buckets[index] = newNode;
} else {
newNode->next = map->buckets[index];
map->buckets[index] = newNode;
}
map->count++;
}
// Search for a house in the hash map
House* findHouse(HashMap* map, const char* address) {
int index = hashFunction(address, map->size);
// Traverse the linked list at the given index
HashNode* current = map->buckets[index];
while (current != NULL) {
if (strcmp(current->key, address) == 0) {
return ¤t->value;
}
current = current->next;
}
// House not found
return NULL;
}
// Main function
int main() {
// Initialize the hash map with a size of 10
HashMap* cityMap = initializeHashMap(10);
// Insert some houses into the hash map
insertHouse(cityMap, "123 Main St", 1);
insertHouse(cityMap, "456 Oak Ave", 2);
insertHouse(cityMap, "789 Pine Rd", 3);
// Search for a house
const char* searchAddress = "Prahlad's Avenue";
House* foundHouse = findHouse(cityMap, searchAddress);
// Display the result
if (foundHouse != NULL) {
printf("House found at %s, House Number: %d\n", foundHouse->address, foundHouse->houseNumber);
} else {
printf("House not found at %s\n", searchAddress);
}
// Free the memory allocated for the hash map
for (int i = 0; i < cityMap->size; i++) {
HashNode* current = cityMap->buckets[i];
while (current != NULL) {
HashNode* next = current->next;
free(current);
current = next;
}
}
free(cityMap->buckets);
free(cityMap);
return 0;
}