Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct lista
  5. {
  6.     int value;
  7.     struct lista *next;
  8.     struct lista *previous;
  9.  
  10. };
  11. struct hash
  12. {
  13.     struct lista *table[200];
  14. };
  15. struct lista *node(int value)
  16. {
  17.     struct lista *new = malloc(sizeof(struct lista));
  18.     new->value=value;
  19.     new->next =NULL;
  20.     new->previous = NULL;
  21.     return new;
  22. }
  23. struct hash *created()
  24. {
  25.     struct hash *new = malloc(sizeof(struct hash));
  26.     int i;
  27.     for(i=0;i<200;i++)
  28.     {
  29.         new->table[i] = NULL;
  30.     }
  31.     return new;
  32. }
  33. void adiciona_atras(struct hash *hash,int value,int key)
  34. {
  35.     int h = value % key;
  36.     struct lista *aux = node(value);
  37.     if(hash->table[h]==NULL)
  38.     {
  39.         hash->table[h]=aux;
  40.  
  41.     }
  42.     else
  43.     {
  44.         hash->table[h]->next = aux;
  45.         aux -> previous = hash->table[h];
  46.         aux ->next = NULL;
  47.         hash->table[h] = aux;
  48.     }
  49.  
  50. }
  51.  
  52. void print_hashalterada(struct hash *hash,int m)
  53. {
  54.     struct lista *aux;
  55.     int i;
  56.     for(i=0;i<m;i++)
  57.     {
  58.         aux = hash -> table[i];
  59.         printf("%d -> ", i);
  60.         if(aux==NULL)
  61.         {
  62.             printf("\\\n");
  63.         }
  64.         while(aux!=NULL)
  65.         {
  66.             if(aux->next==NULL)
  67.             {
  68.                 printf("%d -> \\\n", aux->value);
  69.             }
  70.             else
  71.             {
  72.                 printf("%d -> ", aux->value);
  73.             }
  74.             aux=aux->next;
  75.         }
  76.  
  77.     }
  78. }
  79. int main()
  80. {
  81.     int n,m,c,i,j,number;
  82.     scanf("%d", &n);
  83.     struct hash *hash = created();
  84.     for(i=0;i<n;i++)
  85.     {
  86.         scanf("%d %d", &m, &c);
  87.         for(j=0;j<c;j++)
  88.         {
  89.             scanf("%d", &number);
  90.             adiciona_atras(hash,number,m);
  91.         }
  92.         print_hashalterada(hash,m);
  93.         free(hash);
  94.         struct hash *hash = created();
  95.  
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement