Repository navigation
Expand file tree
/
Copy pathhashtablechaining.h
More file actions
166 lines (149 loc) · 4.77 KB
/
Copy pathhashtablechaining.h
File metadata and controls
166 lines (149 loc) · 4.77 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
#ifndef HASHTABLECHAINING_JL
#define HASHTABLECHAINING_JL
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define HASHTABLE_SIZE 11 // The number of buckets. A prime number spreads the keys better.
// Define how a node of a bucket is structured. Each bucket is a linked list of (key, value) pairs.
typedef struct HashNode{
int key; // The key that identifies the element.
int value; // The value associated to the key.
struct HashNode* next; // The pointer to the next node of the same bucket.
} HashNode;
// Define how our (non linear and dynamic) hash table with chaining is structured.
typedef struct{
HashNode* buckets[HASHTABLE_SIZE]; // The first node of each bucket (null if the bucket is empty).
int size; // The number of (key, value) pairs stored in our table.
} HashTable;
/********************/
/* FUNCTIONS */
/********************/
// Set Up the Hash Table. We start with every bucket empty and size 0.
void initHashTable(HashTable *table){
for(int i = 0; i < HASHTABLE_SIZE; i++){
table->buckets[i] = NULL;
}
table->size = 0;
}
// Calculate the Bucket of a Key. Returns a position between 0 and HASHTABLE_SIZE-1 (also for negative keys).
int hashChaining(int key){
return ((key % HASHTABLE_SIZE) + HASHTABLE_SIZE) % HASHTABLE_SIZE;
}
// Check if the Hash Table is Empty. Returns 1 if it's empty or 0 if it has elements.
int isEmptyHashTable(HashTable *table){
if(table->size == 0){
return 1;
}
else{
return 0;
}
}
// Find the Node of a Key. Returns the node or null if the key is not in the table.
HashNode* findHashNode(HashTable *table, int key){
HashNode* temp = table->buckets[hashChaining(key)];
while(temp != NULL){
if(temp->key == key){
return temp;
}
temp = temp->next;
}
return NULL;
}
// Insert a (key, value) Pair. If the key already exists, its value is updated. Returns 1 if successful or 0 if there's no memory.
int insertHashTable(HashTable *table, int key, int value){
HashNode* node = findHashNode(table, key);
if(node != NULL){
node->value = value;
return 1;
}
HashNode* newNode = (HashNode*)malloc(sizeof(HashNode));
if(!newNode){
return 0;
}
else{
int bucket = hashChaining(key);
newNode->key = key;
newNode->value = value;
newNode->next = table->buckets[bucket];
table->buckets[bucket] = newNode;
table->size++;
return 1;
}
}
// Search for a Key. Returns 1 if found (and its value through the pointer) or 0 if it's not.
int searchHashTable(HashTable *table, int key, int *value){
HashNode* node = findHashNode(table, key);
if(node == NULL){
return 0;
}
else{
*value = node->value;
return 1;
}
}
// Check if a Key is in the Table. Returns 1 if it is or 0 if it's not.
int containsHashTable(HashTable *table, int key){
if(findHashNode(table, key) != NULL){
return 1;
}
else{
return 0;
}
}
// Delete a Key and its Value. Returns 1 if successful or 0 if the key is not found.
int deleteHashTable(HashTable *table, int key){
int bucket = hashChaining(key);
HashNode* temp = table->buckets[bucket];
HashNode* prev = NULL;
while(temp != NULL){
if(temp->key == key){
if(prev == NULL){
table->buckets[bucket] = temp->next;
}
else{
prev->next = temp->next;
}
free(temp);
table->size--;
return 1;
}
prev = temp;
temp = temp->next;
}
return 0;
}
// Get the Number of Nodes in a Bucket. Returns the length of its chain.
int bucketLengthHashTable(HashTable *table, int bucket){
int length = 0;
for(HashNode* temp = table->buckets[bucket]; temp != NULL; temp = temp->next){
length++;
}
return length;
}
// Print every Bucket of the Hash Table. Displays one line per bucket: "3: [(14, 140), (3, 30)]"
void printHashTable(HashTable *table){
for(int i = 0; i < HASHTABLE_SIZE; i++){
printf(" %2d: [", i);
for(HashNode* temp = table->buckets[i]; temp != NULL; temp = temp->next){
printf("(%d, %d)", temp->key, temp->value);
if(temp->next != NULL){
printf(", ");
}
}
printf("]\n");
}
}
// Free all the Nodes and Reset the Table.
void freeHashTable(HashTable *table){
for(int i = 0; i < HASHTABLE_SIZE; i++){
HashNode* temp = table->buckets[i];
while(temp != NULL){
HashNode* nextNode = temp->next;
free(temp);
temp = nextNode;
}
}
initHashTable(table);
}
#endif //HASHTABLECHAINING_JL