karlangadas

ALGORITMIA EX2 2015-1 Pregunta 2

Dec 2nd, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "todo.h"
  5. #define MAX 5
  6.  
  7. int hashfunction(char s[]){
  8.     long long l=strlen(s);
  9.     int suma=0;
  10.     for(long long i=0;i<l;i++){
  11.         suma=suma+s[i]-'A'+1;
  12.     }
  13.     suma=(4+3*suma) % 5;
  14.     return suma;
  15. }
  16.  
  17. void add(TLista *hash,char s[]){
  18.     int k=hashfunction(s);
  19.     Lista_Insertar(&hash[k],s);
  20. }
  21.  
  22. void hashprint(TLista *hash){
  23.     for (int i=0;i<MAX;i++){
  24.         printf("%d: ",i);
  25.         Lista_Imprimir(hash[i]);
  26.     }
  27.  
  28. }
  29. int nelementos=0;
  30. void add2(char hash2[MAX][50],char s[]){
  31.  
  32.     int k=hashfunction(s);
  33.  
  34.     if (nelementos<MAX){
  35.         while (1){
  36.             if (hash2[k][0]=='0'){ 
  37.                 strcpy(hash2[k],s);
  38.                 break;
  39.             }
  40.             k++;
  41.             if (k==MAX)
  42.                 k=0;
  43.         }
  44.         nelementos++;
  45.     }
  46.  
  47. }
  48.  
  49. int main(){
  50.     TLista *hash=malloc(sizeof(TLista)*MAX);
  51.     for (int i=0;i<MAX;i++)
  52.         Lista_Crear(&hash[i]);
  53.    
  54.  
  55.     add(hash,"PERU");
  56.     add(hash,"CHILE");
  57.     add(hash,"BRASIL");
  58.     add(hash,"ARGENTINA");
  59.     add(hash,"BOLIVIA");
  60.     add(hash,"COLOMBIA");
  61.     add(hash,"ECUADOR");
  62.     add(hash,"URUGUAY");
  63.     add(hash,"PARAGUAY");
  64.     add(hash,"VENEZUELA");
  65.     add(hash,"JAMAICA");
  66.     add(hash,"MEXICO");
  67.  
  68.     printf("OPEN HASHING\n");
  69.     hashprint(hash);
  70.     printf("\n");
  71.    
  72.     char hash2[MAX][50];
  73.     memset(hash2,'0',50*MAX);
  74.     add2(hash2,"PERU");
  75.  
  76.     add2(hash2,"CHILE");
  77.     add2(hash2,"BRASIL");
  78.     add2(hash2,"ARGENTINA");
  79.     add2(hash2,"BOLIVIA");
  80.     add2(hash2,"COLOMBIA");
  81.     add2(hash2,"ECUADOR");
  82.     add2(hash2,"URUGUAY");
  83.     add2(hash2,"PARAGUAY");
  84.     add2(hash2,"VENEZUELA");
  85.     add2(hash2,"JAMAICA");
  86.     add2(hash2,"MEXICO");
  87.  
  88.     printf("CLOSED HASHING\n");
  89.     for (int i=0;i<MAX;i++){
  90.         printf("%d: %s\n",i,hash2[i]);
  91.     }
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment