Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct aluno{
  5.      int mat;
  6. };
  7. typedef struct aluno Aluno;
  8.  
  9. struct hash{
  10.     int qtd;    //número de elementos inseridos
  11.     int TABLE_SIZE;  // dimensão da tabela
  12.     Aluno **itens;
  13. };
  14. typedef struct hash Hash;
  15.  
  16. Hash* hsh_cria(void);
  17. Hash* hsh_cria2(int tamanho);
  18. static int hash (Hash* tab, int mat);
  19. Aluno *hsh_insere(Hash *tab,Aluno *a);
  20. static void redimensionar(Hash *tab);
  21.  
  22. Hash* hsh_cria(void){
  23.   int i;
  24.     printf("Funcao Criar \n");
  25.   Hash* tab = (Hash*)malloc(sizeof(Hash));
  26.   tab->qtd = 0;
  27.   tab->TABLE_SIZE = 7; //dimensão inicial
  28.   tab->itens = (Aluno**)malloc(tab->TABLE_SIZE*sizeof(Aluno*));
  29.   for(i=0; i<tab->TABLE_SIZE -1;++i)
  30.       tab->itens[i]=NULL;
  31.   return tab;
  32. }
  33.  
  34. Hash* hsh_cria2(int tamanho){
  35.   int i;
  36.     Hash* tab = (Hash*)malloc(sizeof(Hash));
  37.   tab->qtd = 0;
  38.   tab->TABLE_SIZE = tamanho * 2; //dimensão inicial
  39.   tab->itens = (Aluno**)malloc(tab->TABLE_SIZE*sizeof(Aluno*));
  40.   for(i=0; i<tab->TABLE_SIZE -1;++i)
  41.       tab->itens[i]=NULL;
  42.   return tab;
  43. }
  44.  
  45. static int hash (Hash* tab, int mat){
  46.     printf("Calculando Hash\n");
  47.   return (mat%tab->TABLE_SIZE);
  48. }
  49.  
  50. Aluno* hsh_insere(Hash *tab,Aluno *a){
  51.   printf("Funcao Inserir \n");
  52.   tab->qtd++;
  53.   printf("%i Numeros inseridos na Tabela Hash \n",tab->qtd);
  54.   int controle =  (tab->qtd*100)/tab->TABLE_SIZE;
  55.  
  56.   // verifica porcentagem
  57.   if((tab->qtd*100)/tab->TABLE_SIZE >= 75){
  58.       redimensionar(tab);
  59.   }
  60.  
  61.   // Adiciona aluno na tabela
  62.   tab -> itens[hash(tab, a -> mat)] = a;
  63.  
  64.   return a;
  65. }
  66.  
  67. static void redimensionar(Hash *tab){
  68.   // Tabela auxiliar
  69.   Hash* auxiliar = hsh_cria2(tab -> TABLE_SIZE);
  70.  
  71.   // percorrer a tabela antiga inserido na tabela auxiliar
  72.   for(i=0; i<tab->TABLE_SIZE -1;++i){
  73.     if(tab->itens[i] != NULL){
  74.       hsh_insere(auxiliar, tab -> itens[i]);
  75.     }
  76.   }
  77.   // atualiza tabela antiga
  78.   tab = auxiliar;
  79. }
  80.  
  81. int main(int argc, char const *argv[]) {
  82.  
  83.   return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement