Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct candidato{
- long long int inscr;
- char nome[44];
- int periodo;
- char turno[3];
- int posicao;
- char curso[30];
- };typedef struct candidato Candidato;
- #define N 4073
- // considerando q sao 2036 registros, uma taxa de ocupaçao de 50% precisaria de 4072 posiçoes no vetor. 4073 é o menor numero primo que é maior de 4072
- /* -------------- PARTE DA HASH -------------- */
- struct elementoLista{
- long long inscr;
- int posicao;
- struct elementoLista *prox;
- }; typedef struct elementoLista ElemLista;
- typedef ElemLista* Hash[N]; //define o tipo Hash, um vetor com N posiçoes, com ponteiros para os structs ElemLista
- int hashf(int num){ // funçao que calcula o indice.
- return (num % N);
- }
- void zerarHash(Hash tab){ // bota toda a tabela hash como null, para nao dar problemas depois
- int i;
- for (i = 0; i < N; i++){
- tab[i] = NULL;
- }
- }
- void insereNaHash(long long int inscr, int posicao, Hash tab){
- //pega a inscr e a posiçao em que está e coloca isso na tabela hash
- int h = hashf(inscr);
- ElemLista *aux, *novo = (ElemLista*)malloc(sizeof(ElemLista));
- novo->inscr = inscr; novo->posicao = posicao; novo->prox = NULL;
- if (tab[h] == NULL)
- tab[h] = novo;
- else {
- aux = tab[h];
- tab[h] = novo;
- novo->prox = aux;
- }
- }
- /* -------------- FIM DA PARTE DA HASH -------------- */
- /* -------------- PARTE DO INDICE -------------- */
- /* OBS : O indice guarda a inscriçao e qual a posição dela no arquivo candidatos.dat
- Depois, tendo uma inscr, calcula-se a hash, e vendo no indice, acha-se a posiçao do registro com aquela inscriçao no arquivo original
- Então, vai-se no arquivo original e acessa diretamente aquela posiçao.*/
- struct elementoIndice{
- long long int inscr;
- int posicao;
- };typedef struct elementoIndice ElemIndice;
- typedef ElemIndice* Indice[N]; //vetor com N posiçoes com ponteiros para structs ElemIndice
- void salvarIndice(Hash tab){
- FILE *arq = fopen("candidatos.idx", "w");
- int i;
- for (i = 0; i < N; i++){
- fwrite(tab[i], sizeof(long long int),1, arq);
- fwrite(&i, sizeof(int),1, arq);
- }
- fclose(arq);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement