Advertisement
empyreangov

hash

Sep 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.25 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #define N 100 //Número de valores a serem inseridos na hash
  4.  
  5. using namespace std;
  6.  
  7. //Insere um elemento na hash table.
  8. //Parâmetros: referência para um ponteiro da hash, chave a ser inserida, tamanho, função de hash
  9. int hashInsert(int ** T, int k, int m, int * h(int, int)){
  10.     int i=0;
  11.     do{
  12.         int j = *h(k, i);
  13.         if((*T)[j]==NULL){
  14.             (*T)[j]=k;
  15.             return j;
  16.         }else{
  17.             i++;
  18.         }
  19.     }while(!(i==m));
  20. }
  21.  
  22. //Remove um elemento da hash table.
  23. //Parâmetros: referência para um ponteiro da hash, chave a ser inserida, tamanho, função de hash
  24. int hashSearch(int ** T, int k, int m, int * h(int, int)){
  25.     int i=0;
  26.     int j;
  27.     do{
  28.         j=*h(k, i);
  29.         if((*T)[j]==k){
  30.             return j;
  31.         }
  32.         i++;
  33.     }while(!((*T)[j]==NULL || i==m));
  34.     return NULL;
  35. }
  36.  
  37. //Função de hash de módulo simples
  38. int moduloHash(int key, int size){
  39.     return key%size;
  40. }
  41.  
  42. //Main
  43. int main(int argc, char * argv[]){
  44.     int i;
  45.     if(argc>1){
  46.         int size = atoi(argv[1]);
  47.         int hash[size];
  48.         for(i=0;i<N;i++){
  49.             hashInsert(&hash, i, size, moduloHash);
  50.         }
  51.     }else{
  52.         return -1;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement