Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct lista
- {
- int value;
- struct lista *next;
- struct lista *previous;
- };
- struct hash
- {
- struct lista *table[200];
- };
- struct lista *node(int value)
- {
- struct lista *new = malloc(sizeof(struct lista));
- new->value=value;
- new->next =NULL;
- new->previous = NULL;
- return new;
- }
- struct hash *created()
- {
- struct hash *new = malloc(sizeof(struct hash));
- int i;
- for(i=0;i<200;i++)
- {
- new->table[i] = NULL;
- }
- return new;
- }
- void adiciona_atras(struct hash *hash,int value,int key)
- {
- int h = value % key;
- struct lista *aux = node(value);
- if(hash->table[h]==NULL)
- {
- hash->table[h]=aux;
- }
- else
- {
- hash->table[h]->next = aux;
- aux -> previous = hash->table[h];
- aux ->next = NULL;
- hash->table[h] = aux;
- }
- }
- void print_hashalterada(struct hash *hash,int m)
- {
- struct lista *aux;
- int i;
- for(i=0;i<m;i++)
- {
- aux = hash -> table[i];
- printf("%d -> ", i);
- if(aux==NULL)
- {
- printf("\\\n");
- }
- while(aux!=NULL)
- {
- if(aux->next==NULL)
- {
- printf("%d -> \\\n", aux->value);
- }
- else
- {
- printf("%d -> ", aux->value);
- }
- aux=aux->next;
- }
- }
- }
- int main()
- {
- int n,m,c,i,j,number;
- scanf("%d", &n);
- struct hash *hash = created();
- for(i=0;i<n;i++)
- {
- scanf("%d %d", &m, &c);
- for(j=0;j<c;j++)
- {
- scanf("%d", &number);
- adiciona_atras(hash,number,m);
- }
- print_hashalterada(hash,m);
- free(hash);
- struct hash *hash = created();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement