Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "hash.h"
- #define N 5
- struct aluno{
- int mat;
- char nome[80];
- Aluno* prox;
- };
- struct hash{
- Aluno* v[N];
- };
- static int hash1(int mat){
- return (mat%N)+1;
- }
- Hash* hsh_cria(void){
- Hash* tab = (Hash*)malloc(sizeof(Hash));
- int i;
- for(i=0;i<N;i++){
- tab->v[i] = NULL;
- }
- return tab;
- }
- Aluno* hsh_insere(Hash* tab, char* nome, int mat){
- int h = hash1(mat);
- Aluno* a = (Aluno*)malloc(sizeof(Aluno));
- strcpy(a->nome,nome);
- a->mat = mat;
- tab->v[h] = a;
- return a;
- }
- void hsh_imprime(Hash* tab){
- int i;
- for(i=0;i<N;i++){
- if(tab->v[i]!=NULL)
- printf("%s\t%d\n",tab->v[i]->nome,tab->v[i]->mat);
- }
- }
- Aluno* hsh_busca(Hash* tab, int mat){
- int h = hash1(mat);
- Aluno* a = tab->v[h];
- while(a!=NULL){
- if(a->mat == mat){
- return a;
- }
- a = a->prox;
- }
- return NULL;
- }
- void hsh_libera(Hash* tab){
- Aluno* aux;
- int i;
- for(i=0;i<N;i++){
- while(tab->v[i]!=NULL){
- aux = tab->v[i];
- tab->v[i] = tab->v[i]->prox;
- free(aux);
- }
- }
- free(tab);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement