Advertisement
Guest User

LABORATORIODODESESPEROTEBAYO

a guest
Nov 25th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. /* Nome: Seu nome aqui
  5. * RA: Seu RA aqui
  6. * Laboratorio 09 - Hash */
  7.  
  8. /* STRUCS, DEFINIÇÕES E CABEÇALHOS DAS SUAS FUNÇÕES DA TAD */
  9.  
  10. typedef struct Aluno{
  11.     int ra;
  12.     char name[21];
  13. } aluno;
  14.  
  15. typedef struct No{
  16.     struct Aluno *info;
  17.     struct No *next;
  18. } lista;
  19.  
  20. // assinatura de outras funções
  21. int index(char name[]);
  22. int busca(lista hashtable[], char name[]);
  23. void insert(lista hashtable[], int ra, char name[]);
  24. void remove(lista hashtable[], char name[]);
  25. /* SEU PROGRAMA - MAIN */
  26.  
  27. int main() {
  28.     int ra, i, index, searchAux;
  29.     char input[9], name[21];
  30.     lista hashtable[13];
  31.  
  32.     for(i=0; i<13; i++){
  33.         hashtable[i].next = NULL;
  34.         hashtable[i].info = NULL;
  35.     }
  36.  
  37.     while(1) {
  38.  
  39.         scanf("%s", input);
  40.         if(!strcmp(input,"sair"))
  41.             break;
  42.  
  43.         else if(!strcmp(input,"insere")){
  44.             scanf("%d \"%[^\n\"]s", &ra, name);
  45.             if( busca(hashtable, name) )
  46.                 printf("Nome repetido: \"%s\"\n", name);
  47.             else {
  48.                 printf("Insere: %d \"%s\"\n", ra, name);
  49.                 insert(hashtable, ra, name);
  50.             }
  51.         }
  52.  
  53.         else if(!strcmp(input,"remove")){
  54.             scanf("\"%[^\n\"]s", name);
  55.             printf("Remove: \"%s\"\n", name);
  56.             if( !busca(hashtable, name) )
  57.                 printf("Nome inexistente: \"%s\"\n", name);
  58.             else {
  59.                 printf("Remove: \"%s\"\n", name);
  60.                 remove(hashtable, name);
  61.             }
  62.         }
  63.  
  64.         else if(!strcmp(input,"consulta")){
  65.             scanf("\"%[^\n\"]s", name);
  66.             printf("Consulta: \"%s\"\n", name);
  67.             searchAux = busca(hashtable, name);
  68.             if(searchAux)
  69.                 printf("%d \"%s\"\n", searchAux, name);
  70.             else
  71.                 printf("Nome inexistente: \"%s\"\n", name);
  72.         }
  73.  
  74.         else if(!strcmp(input,"imprime")){
  75.             printf("%s %d\n", name);
  76.         }
  77.     }
  78.  
  79.     return 0;
  80. }
  81.  
  82. /* IMPLEMENTAÇÃO DAS FUNÇÕES DE SUA TAD */
  83. int hash(char name[]){
  84.     int XOR, i;
  85.     XOR = name[0];
  86.  
  87.     for(i=1; name[i] != '\0'; i++)
  88.         XOR ^= name[i];
  89.  
  90.     return XOR%13;
  91. }
  92.  
  93. int busca(lista hashtable[], char name[]){
  94.     int i, index;
  95.  
  96.     index = hash(name);
  97.     lista *aux = &hashtable[index];
  98.     printf("Busca iniciada\n");
  99.  
  100.     if(aux->info){
  101.         printf("Existe Informacao...\n");
  102.             if(!strcmp(aux->info->name, name))
  103.                 return aux->info->ra;
  104.     }
  105.  
  106.     while(aux->next){
  107.         if(aux->info){
  108.             printf("Existe Informacao...\n");
  109.             if(!strcmp(aux->info->name, name))
  110.                 return aux->info->ra;
  111.         }
  112.         else
  113.             return 0;
  114.  
  115.         aux = aux->next;
  116.     }
  117.  
  118.     return 0;
  119. }
  120.  
  121. void insert(lista hashtable[], int ra, char name[]){
  122.     int index;
  123.  
  124.     index = hash(name);
  125.     lista *aux = &hashtable[index];
  126.     lista *novoAluno = NULL;
  127.  
  128.     printf("Iniciando insercao\n");
  129.  
  130.     if(!(aux->info)){
  131.         printf("Nao existia informacao nesse XOR\n");
  132.         aux->info = (aluno*)malloc(sizeof(aluno));
  133.         aux->info->ra = ra;
  134.         strcpy(aux->info->name, name);
  135.         printf("O slot %d agora tem o nome %s\n", index-1, hashtable[index].info->name);
  136.         return;
  137.     }
  138.  
  139.     while(aux->next){
  140.         if(strcmp(aux->next->info->name, name)<0)
  141.             aux = aux->next;
  142.     }
  143.  
  144.     novoAluno = (lista*)malloc(sizeof(lista));
  145.     novoAluno->next = aux->next;
  146.     aux->next = novoAluno;
  147.     novoAluno->info = (aluno*)malloc(sizeof(aluno));
  148.     novoAluno->info->ra = ra;
  149.     strcpy(novoAluno->info->name, name);
  150.     return;
  151. }
  152.  
  153. void remove(lista hashtable[], char name[]){
  154.     int index;
  155.  
  156.     index = hash(name);
  157.     lista *aux = hashtable[index];
  158.  
  159.     if(!strcmp(aux->info->name, name){
  160.  
  161.     }
  162.     else{
  163.         while(strcmp(aux->info->name, name)){
  164.             aux = aux-> next;
  165.             printf("iterando...\n");
  166.         }
  167.     }
  168.  
  169.  
  170.  
  171.  
  172.     return;
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement