Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "hash.h"
  5.  
  6. #define N 5
  7.  
  8. struct aluno{
  9. int mat;
  10. char nome[80];
  11. Aluno* prox;
  12. };
  13.  
  14. struct hash{
  15. Aluno* v[N];
  16. };
  17.  
  18. static int hash1(int mat){
  19. return (mat%N)+1;
  20. }
  21.  
  22. Hash* hsh_cria(void){
  23. Hash* tab = (Hash*)malloc(sizeof(Hash));
  24. int i;
  25. for(i=0;i<N;i++){
  26. tab->v[i] = NULL;
  27. }
  28. return tab;
  29. }
  30.  
  31. Aluno* hsh_insere(Hash* tab, char* nome, int mat){
  32. int h = hash1(mat);
  33. Aluno* a = (Aluno*)malloc(sizeof(Aluno));
  34. strcpy(a->nome,nome);
  35. a->mat = mat;
  36. tab->v[h] = a;
  37. return a;
  38. }
  39.  
  40. void hsh_imprime(Hash* tab){
  41. int i;
  42. for(i=0;i<N;i++){
  43. if(tab->v[i]!=NULL)
  44. printf("%s\t%d\n",tab->v[i]->nome,tab->v[i]->mat);
  45. }
  46. }
  47.  
  48. Aluno* hsh_busca(Hash* tab, int mat){
  49. int h = hash1(mat);
  50. Aluno* a = tab->v[h];
  51. while(a!=NULL){
  52. if(a->mat == mat){
  53. return a;
  54. }
  55. a = a->prox;
  56. }
  57. return NULL;
  58. }
  59.  
  60. void hsh_libera(Hash* tab){
  61. Aluno* aux;
  62. int i;
  63. for(i=0;i<N;i++){
  64. while(tab->v[i]!=NULL){
  65. aux = tab->v[i];
  66. tab->v[i] = tab->v[i]->prox;
  67. free(aux);
  68. }
  69. }
  70. free(tab);
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement