Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct aluno{
- int mat;
- };
- typedef struct aluno Aluno;
- struct hash{
- int qtd; //número de elementos inseridos
- int TABLE_SIZE; // dimensão da tabela
- Aluno **itens;
- };
- typedef struct hash Hash;
- Hash* hsh_cria(void);
- Hash* hsh_cria2(int tamanho);
- static int hash (Hash* tab, int mat);
- Aluno *hsh_insere(Hash *tab,Aluno *a);
- static void redimensionar(Hash *tab);
- Hash* hsh_cria(void){
- int i;
- printf("Funcao Criar \n");
- Hash* tab = (Hash*)malloc(sizeof(Hash));
- tab->qtd = 0;
- tab->TABLE_SIZE = 7; //dimensão inicial
- tab->itens = (Aluno**)malloc(tab->TABLE_SIZE*sizeof(Aluno*));
- for(i=0; i<tab->TABLE_SIZE -1;++i)
- tab->itens[i]=NULL;
- return tab;
- }
- Hash* hsh_cria2(int tamanho){
- int i;
- Hash* tab = (Hash*)malloc(sizeof(Hash));
- tab->qtd = 0;
- tab->TABLE_SIZE = tamanho * 2; //dimensão inicial
- tab->itens = (Aluno**)malloc(tab->TABLE_SIZE*sizeof(Aluno*));
- for(i=0; i<tab->TABLE_SIZE -1;++i)
- tab->itens[i]=NULL;
- return tab;
- }
- static int hash (Hash* tab, int mat){
- printf("Calculando Hash\n");
- return (mat%tab->TABLE_SIZE);
- }
- Aluno* hsh_insere(Hash *tab,Aluno *a){
- printf("Funcao Inserir \n");
- tab->qtd++;
- printf("%i Numeros inseridos na Tabela Hash \n",tab->qtd);
- int controle = (tab->qtd*100)/tab->TABLE_SIZE;
- // verifica porcentagem
- if((tab->qtd*100)/tab->TABLE_SIZE >= 75){
- redimensionar(tab);
- }
- // Adiciona aluno na tabela
- tab -> itens[hash(tab, a -> mat)] = a;
- return a;
- }
- static void redimensionar(Hash *tab){
- // Tabela auxiliar
- Hash* auxiliar = hsh_cria2(tab -> TABLE_SIZE);
- // percorrer a tabela antiga inserido na tabela auxiliar
- for(i=0; i<tab->TABLE_SIZE -1;++i){
- if(tab->itens[i] != NULL){
- hsh_insere(auxiliar, tab -> itens[i]);
- }
- }
- // atualiza tabela antiga
- tab = auxiliar;
- }
- int main(int argc, char const *argv[]) {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement