Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "todo.h"
- #define MAX 5
- int hashfunction(char s[]){
- long long l=strlen(s);
- int suma=0;
- for(long long i=0;i<l;i++){
- suma=suma+s[i]-'A'+1;
- }
- suma=(4+3*suma) % 5;
- return suma;
- }
- void add(TLista *hash,char s[]){
- int k=hashfunction(s);
- Lista_Insertar(&hash[k],s);
- }
- void hashprint(TLista *hash){
- for (int i=0;i<MAX;i++){
- printf("%d: ",i);
- Lista_Imprimir(hash[i]);
- }
- }
- int nelementos=0;
- void add2(char hash2[MAX][50],char s[]){
- int k=hashfunction(s);
- if (nelementos<MAX){
- while (1){
- if (hash2[k][0]=='0'){
- strcpy(hash2[k],s);
- break;
- }
- k++;
- if (k==MAX)
- k=0;
- }
- nelementos++;
- }
- }
- int main(){
- TLista *hash=malloc(sizeof(TLista)*MAX);
- for (int i=0;i<MAX;i++)
- Lista_Crear(&hash[i]);
- add(hash,"PERU");
- add(hash,"CHILE");
- add(hash,"BRASIL");
- add(hash,"ARGENTINA");
- add(hash,"BOLIVIA");
- add(hash,"COLOMBIA");
- add(hash,"ECUADOR");
- add(hash,"URUGUAY");
- add(hash,"PARAGUAY");
- add(hash,"VENEZUELA");
- add(hash,"JAMAICA");
- add(hash,"MEXICO");
- printf("OPEN HASHING\n");
- hashprint(hash);
- printf("\n");
- char hash2[MAX][50];
- memset(hash2,'0',50*MAX);
- add2(hash2,"PERU");
- add2(hash2,"CHILE");
- add2(hash2,"BRASIL");
- add2(hash2,"ARGENTINA");
- add2(hash2,"BOLIVIA");
- add2(hash2,"COLOMBIA");
- add2(hash2,"ECUADOR");
- add2(hash2,"URUGUAY");
- add2(hash2,"PARAGUAY");
- add2(hash2,"VENEZUELA");
- add2(hash2,"JAMAICA");
- add2(hash2,"MEXICO");
- printf("CLOSED HASHING\n");
- for (int i=0;i<MAX;i++){
- printf("%d: %s\n",i,hash2[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment