#include <stdio.h>
#include <stdlib.h>
// Estrutura do nó para encadeamento externo
typedef struct No {
int chave;
struct No* prox;
} No;
// Função para criar um novo nó
No* criarNo(int chave) {
No
* novoNo
= (No
*)malloc(sizeof(No
)); novoNo->chave = chave;
novoNo->prox = NULL;
return novoNo;
}
// Função para inserir uma chave na tabela de dispersão
void inserir(No** tabelaHash, int chave, int m) {
int k = chave % m;
No* novoNo = criarNo(chave);
if (tabelaHash[k] == NULL) {
tabelaHash[k] = novoNo;
} else {
No* temp = tabelaHash[k];
while (temp->prox != NULL) {
temp = temp->prox;
}
temp->prox = novoNo;
}
}
// Função para imprimir a tabela de dispersão
void imprimirTabelaHash(No** tabelaHash, int m) {
for (int i = 0; i < m; i++) {
No* temp = tabelaHash[i];
while (temp != NULL) {
printf(" %d ->", temp
->chave
); temp = temp->prox;
}
}
}
// Função para liberar a memória da tabela de dispersão
void liberarTabelaHash(No** tabelaHash, int m) {
for (int i = 0; i < m; i++) {
No* temp = tabelaHash[i];
while (temp != NULL) {
No* paraLiberar = temp;
temp = temp->prox;
}
}
}
int main() {
int n;
for (int i = 0; i < n; i++) {
int m, c;
No
** tabelaHash
= (No
**)malloc(m
* sizeof(No
*)); for (int j = 0; j < m; j++) {
tabelaHash[j] = NULL;
}
for (int j = 0; j < c; j++) {
int chave;
inserir(tabelaHash, chave, m);
}
imprimirTabelaHash(tabelaHash, m);
liberarTabelaHash(tabelaHash, m);
if (i < n - 1) {
}
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCi8vIEVzdHJ1dHVyYSBkbyBuw7MgcGFyYSBlbmNhZGVhbWVudG8gZXh0ZXJubwp0eXBlZGVmIHN0cnVjdCBObyB7CiAgICBpbnQgY2hhdmU7CiAgICBzdHJ1Y3QgTm8qIHByb3g7Cn0gTm87CgovLyBGdW7Dp8OjbyBwYXJhIGNyaWFyIHVtIG5vdm8gbsOzCk5vKiBjcmlhck5vKGludCBjaGF2Z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FxuIik7CiAgICB9Cn0KCi8vIEZ1bsOnw6NvIHBhcmEgbGliZXJhciBhIG1lbcOzcmlhIGRhIHRhYmVsYSBkZSBkaXNwZXJzw6NvCnZvaWQgbGliZXJhclRhYmVsYUhhc2goTm8qKiB0YWJlbGFIYXNoLCBpbnQgbSkgewogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpKyspIHsKICAgICAgICBObyogdGVtcCA9IHRhYmVsYUhhc2hbaV07CiAgICAgICAgd2hpbGUgKHRlbXAgIT0gTlVMTCkgewogICAgICAgICAgICBObyogcGFyYUxpYmVyYXIgPSB0ZW1wOwogICAgICAgICAgICB0ZW1wID0gdGVtcC0+cHJveDsKICAgICAgICAgICAgZnJlZShwYXJhTGliZXJhcik7CiAgICAgICAgfQogICAgfQp9CgppbnQgbWFpbigpIHsKICAgIGludCBuOwogICAgc2NhbmYoIiVkIiwgJm4pOwoKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgaW50IG0sIGM7CiAgICAgICAgc2NhbmYoIiVkICVkIiwgJm0sICZjKTsKCiAgICAgICAgTm8qKiB0YWJlbGFIYXNoID0gKE5vKiopbWFsbG9jKG0gKiBzaXplb2YoTm8qKSk7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgdGFiZWxhSGFzaFtqXSA9IE5VTEw7CiAgICAgICAgfQoKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8IGM7IGorKykgewogICAgICAgICAgICBpbnQgY2hhdmU7CiAgICAgICAgICAgIHNjYW5mKCIlZCIsICZjaGF2ZSk7CiAgICAgICAgICAgIGluc2VyaXIodGFiZWxhSGFzaCwgY2hhdmUsIG0pOwogICAgICAgIH0KCiAgICAgICAgaW1wcmltaXJUYWJlbGFIYXNoKHRhYmVsYUhhc2gsIG0pOwoKICAgICAgICBsaWJlcmFyVGFiZWxhSGFzaCh0YWJlbGFIYXNoLCBtKTsKICAgICAgICBmcmVlKHRhYmVsYUhhc2gpOwoKICAgICAgICBpZiAoaSA8IG4gLSAxKSB7CiAgICAgICAgICAgIHByaW50ZigiXG4iKTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIDA7Cn0K