Advertisement
cbmelo97

Untitled

Sep 9th, 2018
3,111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct lista{
  5.     int num;
  6.     struct lista *prox;
  7. }aLista;
  8.  
  9. void addItem(aLista **listinha, int nk, int hash){
  10.     aLista *ponteiro = (aLista *)malloc(sizeof(aLista));
  11.     ponteiro -> num = nk;
  12.     ponteiro -> prox = NULL;
  13.     if(listinha[hash] == NULL){
  14.         listinha[hash] = ponteiro;
  15.     }else{
  16.         aLista *aux = listinha[hash];
  17.         while (aux->prox != NULL){
  18.             aux = aux->prox;
  19.         }
  20.         aux -> prox = ponteiro;
  21.     }
  22. }
  23.  
  24. void Item(aLista **listinha, int i){
  25.     printf("%d -> ",i);
  26.     aLista *ponteiro = listinha[i];
  27.     while(ponteiro != NULL){
  28.         printf("%d", ponteiro->num);
  29.         ponteiro = ponteiro->prox;
  30.     };
  31.     printf("\\\n");
  32. }
  33.  
  34. int main(void){
  35.     int nt, tl, nk;
  36.     scanf("%d",&nt);
  37.    
  38.     while(nt--){
  39.         aLista *listinha[99] = {NULL};
  40.         scanf("%d %d", &tl,&nk);
  41.    
  42.         for(; nk > 0;nk--){
  43.             int chave;
  44.             scanf("%d", &chave);
  45.             int hash = chave % tl;
  46.             addItem(listinha,nk,hash);
  47.         }
  48.  
  49.         for (int i = 0; i < tl; i++){
  50.             Item(listinha, i);
  51.         }
  52.  
  53.         if(nt > 0){
  54.             printf("\n");
  55.         }
  56.  
  57.     }
  58.     return 0;
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement