-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashTable.c
More file actions
210 lines (175 loc) · 5.66 KB
/
Copy pathhashTable.c
File metadata and controls
210 lines (175 loc) · 5.66 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
#include "hashTable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static HashTable* hashTable = NULL;
void initializeHashTable(int size) {
hashTable = (HashTable*)malloc(sizeof(HashTable));
hashTable->size = size;
hashTable->count = 0;
hashTable->table = (Slot**)malloc(sizeof(Slot*) * size);
// Inicializar las entradas de la tabla
for (int i = 0; i < size; i++) {
hashTable->table[i] = NULL;
}
}
// Función de hash
int hash(const char* token) {
unsigned long hashValue = 0;
int c;
while ((c = *token++)) {
hashValue = (hashValue * 31) + c; // Función de hash básica
}
return hashValue % hashTable->size;
}
// Buscar un token en la tabla hash
Slot search(const char* token) {
int index = hash(token);
Slot* s = hashTable->table[index];
Slot result;
result.id = -10; // Token no encontrado
result.token = NULL;
result.value = 0.0;
while (s) {
if (strcmp(s->token, token) == 0) {
result.id = s->id;
result.token = strdup(s->token); // OK
result.value = s->value; // ✅ añadimos esto
break;
}
s = s->next;
}
return result;
}
// Insertar un token en la tabla hash
void insert(const char* token, int id, double value) {
// Si la tabla está demasiado llena, la redimensionamos
if (hashTable->count >= hashTable->size * 0.7) { // Umbral del 70%
resizeHashTable(hashTable);
}
int index = hash(token);
// Buscar si el token ya existe
if (search(token).id != -10) {
return; // El token ya está en la tabla
}
// Si no existe, crear un nuevo slot y agregarlo
Slot* newSlot = (Slot*)malloc(sizeof(Slot));
newSlot->token = strdup(token);
if (newSlot->token == NULL) {
printf("Error al asignar memoria para el token.\n");
free(newSlot);
return;
}
newSlot->id = id; // Asignar el ID
newSlot->value = value;
newSlot->next = hashTable->table[index]; // Insertar al principio de la lista
hashTable->table[index] = newSlot;
hashTable->count++; // Incrementar el contador de elementos
}
int update(const char* token, double newValue) {
int index = hash(token);
Slot* s = hashTable->table[index];
while (s) {
if (strcmp(s->token, token) == 0) {
s->value = newValue;
return 1;
}
s = s->next;
}
return 0;
}
// Función para redimensionar la tabla hash
void resizeHashTable() {
int newSize = hashTable->size * 2; // Doblamos el tamaño de la tabla
Slot** newTable = (Slot**)malloc(sizeof(Slot*) * newSize);
// Inicializar la nueva tabla
for (int i = 0; i < newSize; i++) {
newTable[i] = NULL;
}
// Reinsertar los elementos en la nueva tabla
for (int i = 0; i < hashTable->size; i++) {
Slot* slot = hashTable->table[i];
while (slot != NULL) {
int newIndex = hash(slot->token) % newSize; // Recalcular el hash con el nuevo tamaño
Slot* newSlot = (Slot*)malloc(sizeof(Slot));
newSlot->token = strdup(slot->token);
newSlot->id = slot->id;
newSlot->next = newTable[newIndex];
newTable[newIndex] = newSlot;
slot = slot->next;
}
}
// Liberar la memoria de la tabla vieja
free(hashTable->table);
hashTable->table = newTable;
hashTable->size = newSize;
}
// Imprimir toda la tabla hash
void printTS() {
if (hashTable == NULL) {
printf("La tabla de símbolos no está inicializada.\n");
return;
}
if (hashTable->size == 0) {
printf("\n----- Workspace vacío -----\n");
}
else {
printf("\n----- Variables definidas -----\n");
for (int i = 0; i < hashTable->size; i++) {
Slot* slot = hashTable->table[i];
while (slot != NULL) {
if (slot->id == T_ID) {
printf("%s = %f\n", slot->token, slot->value);
}
slot = slot->next;
}
}
printf("-------------------------------\n");
}
}
void clearType(int id) {
if (hashTable == NULL) return;
for (int i = 0; i < hashTable->size; i++) {
Slot* current = hashTable->table[i];
Slot* prev = NULL;
while (current != NULL) {
if (current->id == id) {
// Eliminar este nodo
Slot* toDelete = current;
if (prev == NULL) {
hashTable->table[i] = current->next;
current = hashTable->table[i];
} else {
prev->next = current->next;
current = current->next;
}
free(toDelete->token);
free(toDelete);
hashTable->count--;
} else {
prev = current;
current = current->next;
}
}
}
}
// Liberar la memoria de la tabla hash
void freeHashTable() {
if (hashTable == NULL) {
return;
}
// Liberar cada "Slot" en la tabla
for (int i = 0; i < hashTable->size; i++) {
Slot* slot = hashTable->table[i];
while (slot != NULL) {
Slot* temp = slot; // Guardar el nodo actual
slot = slot->next; // Mover al siguiente nodo
free(temp->token); // Liberar el token (cadena)
free(temp); // Liberar el nodo Slot
}
}
// Liberar la tabla en sí
free(hashTable->table);
free(hashTable); // Liberar la estructura de la tabla hash
hashTable = NULL; // Asegurar que el puntero se ponga a NULL después de liberarlo
}