/* intercalar duas listas ordenadas*/ void intercala(lista *l1, lista*l2, lista *l3){ no *aux; no *aux2; int i,j; aux = l1 -> ini; aux2 = l2 -> ini; i = j = 0; while(i < (l1 -> tam) || j < (l2 -> tam)){ if(i < (l1 -> tam)){ if(j < (l2 -> tam)){ if((aux -> conteudo) < (aux2 -> conteudo)){ insereFim(l3,aux); aux = aux -> prox; i++; }else{ insereFim(l3,aux2); aux2 = aux2 -> prox; j++; } }else{ insereFim(l3,aux); aux = aux -> prox; i++; } }else{ insereFim(l3,aux2); aux2 = aux2 -> prox; j++; } } }
/* intercalar duas listas ordenadas*/ void imparPar(lista *l1, lista*impar, lista *par){ no *aux; int i; aux = l1 -> ini; i = 0; while(i < (l1 -> tam)){ if((aux -> conteudo) % 2 == 0 ){ insereFim(par,aux); }else{ insereFim(impar,aux); } aux = aux -> prox; i++; } }
int main(){ int num,num2, opcao,mod,div; Lista l,l2,l3; inicLista(&l); inicLista(&l2); inicLista(&l3); printf("Entre com o primeiro numero: "); scanf("%d", &num); while(num > 0) { mod = num % 10000; num = num / 10000; //printf("Num: %d\n",num); insereFim(&l, mod); } ///exibe(&l); printf("\nEntre com o segundo numero: "); scanf("%d", &num2); while(num2 > 0) { mod = num2 % 10000; num2 = num2 / 10000; //printf("Num2: %d\n",num2); insereFim(&l2, mod); } soma(&l,&l2,&l3); //exibe(&l2); printf("\n"); printf("Lista 1: \n"); exibe(&l); printf("\n\n"); printf("Lista 2: \n"); exibe(&l2); printf("\n\n"); printf("Soma das duas Listas: \n"); exibe(&l3); printf("\n\n"); libera(&l); libera(&l2); libera(&l3); return 0; }
int main(int argc, char **argv) { no *n; lista *l; lista *l2; int i; l = criaLista(); l2 = criaLista(); for(i = 1 ; i < 5 ; i++){ n = criaNo(i); insereFim(l,n); } for(i = 1 ; i < 5 ; i++){ n = criaNo(i); insereIni(l2,n); } printf("\n ***inserindo no fim e imprimindo*** \n"); imprimeLista(l); printf("\n ***inserindo no inicio e imprimindo*** \n"); imprimeLista(l2); liberaLista(l);/* libernado as listas*/ liberaLista(l2); /* libernado as listas*/ return 0; }
void opcao(node *LISTA, int op) { switch(op){ case 0: libera(LISTA); break; case 1: exibe(LISTA); break; case 2: insereInicio(LISTA); break; case 3: insereFim(LISTA); break; case 4: inicia(LISTA); break; default: printf("Comando invalido\n\n"); } }
lista *inserePosN(lista *inicio){ lista *novo, *anterior, *proximo; int num, pos; int numLista = contaLista(inicio); do{ printf("\nInsira a posicao da lista na qual voce deseja adicionar um nodo: "); scanf("%d", &pos); if (pos < 0 || pos > numLista+1){ printf("\nA lista possui %d posicoes, insira uma posicao compativel para inserir um nodo!"); } } while (pos < 0 || pos > numLista+1); if (isPrimeiro(pos)){ inicio = insereInicio(inicio); } else if (pos == numLista+1){ inicio = insereFim(inicio); } else{ printf("\nDigite um valor para inserir no novo nodo: "); scanf("%d", &num); novo = getMallocLista(); anterior = pegaNodoN(inicio, pos - 1); proximo = pegaNodoN(inicio, pos + 1); if (novo != 0){ novo->valor = num; novo->prox = anterior->prox; anterior->prox = novo; } imprimeLista(inicio, pos, 0, 1); } return inicio; }
void copia(Lista *l_Origem, Lista *l_Copia){ Nodo *atual = (*l_Origem); criaLista(l_Copia); while(atual != NULL){ insereFim(l_copia, atual->info); atual = atual->prox; } }
/*concatenar duas listas circulares*/ void concatena(lista *l1, lista *l2, lista *l3){ no *aux; int i,j; aux = l1->ini; i = j = 0; while(i < (l1 -> tam)){ insereFim(l3,aux); aux = aux -> prox; i++; } aux = l2->ini; while(j < (l2 -> tam)){ insereFim(l3,aux); aux = aux -> prox; j++; } }
int main(int argc, char **argv) { no *n; lista *l; lista *l2; lista *l3; int i,k; l = criaLista(); l2 = criaLista(); l3 = criaLista(); for(i = 1 ; i < 5 ; i++){ n = criaNo(i); insereFim(l,n); } for(i = 2 ; i < 6 ; i++){ n = criaNo(i); insereFim(l2,n); } printf("\n **lista ** \n"); imprimeLista(l); printf("\n **lista 2** \n"); imprimeLista(l2); printf("\n **uniao ** \n"); uniao(l,l2,l3); imprimeLista(l3); liberaLista(l3); l3 = criaLista(); printf("\n **intersecao ** \n"); intersecao(l,l2,l3); imprimeLista(l3); k = pertence(l,l2); printf("\n \n 0 - NAO \n 1 - SIM \n"); printf("pertence: %d \n",k); liberaLista(l); liberaLista(l2); liberaLista(l3); return 0; }
void uniao(lista *l1, lista *l2, lista *l3){ no *aux; no *aux2; int i,j; aux = l1->ini; i = j = 0; while(i < (l1 -> tam)){ aux2 = criaNo(aux->conteudo); insereFim(l3,aux2); aux = aux -> prox; i++; } aux = l2->ini; while(j < (l2 -> tam)){ aux2 = criaNo(aux -> conteudo); insereFim(l3,aux2); aux = aux -> prox; j++; } }
/* Insere um elemento no final da lista */ void insereFim(Lista *p_l, elem_t e){ No_lista *novo; if (p_l->prox == NULL){ novo = malloc (sizeof(No_lista)); novo->info = e; novo->prox = NULL; p_l->prox = novo; } else insereFim(p_l->prox, e); }
void ordenaListaDescrescente(lista *l1, lista *l2){ int cont, cont2, verifica; no *aux; no *aux2; no *pega; aux = l1 -> ini; pega = criaNo(aux -> conteudo); insereFim(l2,pega); aux = aux -> prox; cont = 1; while(cont < l1 -> tam ){ aux2 = l2 -> ini; cont2 = 0; verifica = 0; while(cont2 < l2 -> tam ){ if(aux->conteudo > aux2 -> conteudo){ if(cont2 == 0){ pega = criaNo(aux -> conteudo); insereIni(l2,pega); verifica++; cont2 = l2 -> tam; }else{ pega = criaNo(aux -> conteudo); insereEsq(l2,pega,cont2); verifica++; cont2 = l2 -> tam; } } cont2++; aux2 = aux2 -> prox; } if(verifica == 0){ pega = criaNo(aux -> conteudo); insereFim(l2,pega); } cont++; aux = aux -> prox; } }
/* fazer uma cópia da lista*/ void copiaLista(lista *l1, lista *l2){ no *aux; no *pega; int i; liberaLista(l1); l1 = criaLista(); aux = l2 -> ini; i = 0; while(i < (l2 -> tam)){ pega = criaNo(aux -> conteudo); insereFim(l1,pega); aux = aux -> prox; i++; } }
int main(int argc, char** argv) { lista *ptri = NULL; int menu = 1; printf("************************************************"); printf("\n* OPERACOES COM LISTAS SIMPLESMENTE ENCADEADAS *"); printf("\n************************************************"); printf("\nPressione ENTER para continuar.."); getch(); while (menu != 0){ if (ptri != NULL){ menu = imprimeMenu(); switch (menu){ case 1: ptri = insereInicio(ptri); break; case 2: ptri = insereFim(ptri); break; case 3: ptri = inserePosN(ptri); break; case 4: trocaPos(ptri); break; case 5: ptri = deletaNodo(ptri); break; case 6: imprimeLista(ptri, 0, 0, 1); break; }//fecha switch menu }//fecha if else{ ptri = inserePrimeiro(ptri); } }//fecha while return (EXIT_SUCCESS); }
void cadListCategoria(Categoria *listCat,int *quant) { if(!listCat) { printf("Sem memoria!\n"); //Provocando uma saída do sistema caso a memória que precisemos não seja alocada. exit(1); } //printf("\n\ncadListCategoria[%p]\n\n",listCat); insereFim(listCat); (*quant)++; printf("\nTotal de categorias: %d\n\n",*quant); system("pause"); }
int main(){ int i; lista l; no n; criaLista(&l); for(i = 0; i < 10; i++){ criaNo(&n,i); insereFim(&l,n); /* inserindo no Fim*/ } printf("\n *** removendo a 2 pos***\n"); removePos(&l,2); imprimeLista(&l); liberaLista(&l); return 0; }
int main(){ int i; lista l; no n; criaLista(&l); for(i = 0; i < 10; i++){ criaNo(&n,i); insereFim(&l,n); } criaNo(&n,5); insereNCopias(&l,n,2,4); printf("\n**INSERE NCOPIAS\n"); imprimeLista(&l); liberaLista(&l); return 0; }
/* inserindo na posicao */ void inserePos(lista *l, no *n,int t){ int i; no *aux; if(t == 1) { insereIni(l,n); return; } if(t == l -> tam + 1) { insereFim(l,n); return; } for( i = 0 , aux = l -> ini; i < t - 1; aux = aux -> prox , i++); n->prox = aux -> prox; aux->prox = n; l -> tam++; }
/* procedimento troca (k, L , v) – modifica o valor do k-ésimo elemento da lista para v. */ void troca(lista *l, no *n,int t){ int i; no *aux; if(t == 1) { insereIni(l,n); return; } if(t == l -> tam + 1) { insereFim(l,n); return; } for( i = 0 , aux = l -> ini; i < t - 1; aux = aux -> prox , i++); n->prox = aux -> prox; aux->conteudo = n->conteudo; free(n); }
int main(int argc, char **argv) { int i; lista *l; no *n; l = criaLista(); for(i = 0; i < 5; i++){ n = criaNo(i); insereFim(l,n); } printf("\n**lista normal**\n"); imprimeLista(l); //~ trocaPos(l,2,3); trocaPos(l,2,3); printf("\n**lista trocaPos**\n"); imprimeLista(l); //~ liberaLista(l); return 0; }
void intersecao(lista *l1, lista *l2, lista *l3){ int cont, cont2; no *aux; no *aux2; no *pega; cont = 0; aux = l1 -> ini; while(cont < l1 -> tam ){ aux2 = l2 -> ini; cont2 = 0; while(cont2 < l2 -> tam ){ if(aux->conteudo == aux2 -> conteudo){ pega = criaNo(aux -> conteudo); insereFim(l3,pega); } cont2++; aux2 = aux2 -> prox; } cont++; aux = aux -> prox; } }
void intersecao2(lista *l1, lista *l2, lista *l4){ int cont, cont2; no *aux; no *aux2; no *pega; cont = 0; aux = l2 -> ini; while(cont < l2 -> tam ){ aux2 = l1 -> ini; cont2 = 0; while(cont2 < l1 -> tam ){ pega = buscaPos(l4,aux2); if(pega == NULL){ pega = criaNo(aux2 -> conteudo); insereFim(l4,pega); } cont2++; aux2 = aux2 -> prox; } cont++; aux = aux -> prox; } }
int main(){ int num, opcao; Lista l; inicLista(&l); opcao = 1; while(opcao <= 10 && opcao > 0){ printf("\n 1 - insere um numero no inicio da lista"); printf("\n 2 - insere um numero no fim da lista"); printf("\n 3 - insere um numero de maneira ordenada"); printf("\n 4 - verifica se a lista esta ordenada"); printf("\n 5 - ordena a lista"); printf("\n 6 - remove o elemento que esta no inicio da lista"); printf("\n 7 - remove o elemento que esta no fim da lista"); printf("\n 8 - remove um valor determinado"); printf("\n 9 - inverte"); printf("\n 10 - exibe a lista"); printf("\n qualquer outro numero para sair"); printf("\n\nEntre com uma das opcoes acima: "); scanf("%d", &opcao); switch (opcao){ case 1: printf("\n\nEntre com o numero a ser inserido: "); scanf("%d", &num); insereInicio(&l, num); break; case 2: printf("\n\nEntre com o numero a ser inserido: "); scanf("%d", &num); insereFim(&l, num); break; case 3: printf("\n\nEntre com o numero a ser inserido: "); scanf("%d", &num); insereOrdenado(&l, num); break; case 4: if (ordenada(&l)) printf("\nLista ordenada\n"); else printf("\nLista desordenada\n"); break; case 5: ordena(&l); break; case 6: if (removeInicio(&l, &num)) printf("\nNumero removido: %d\n", num); else printf("\nLista vazia"); break; case 7: if (removeFim(&l, &num)) printf("\nNumero removido: %d\n", num); else printf("\nLista vazia"); break; case 8: printf("\n\nEntre com o numero a ser removido: "); scanf("%d", &num); if (!removeValor(&l, num)) printf("Numero nao encontrado"); break; case 9: inverte(&l); break; case 10: exibe(&l); break; } exibe(&l); printf("\n\n"); } // libera(&l); return 0; }
void insereLista(Lista *p_l, elem_t e) { if (e >= 60) return inserePrioridade(p_l, e); else return insereFim(p_l, e); }
/** * Remove chave * @param chave a ser removida * @return */ void remover(int chave) { int salto, index_aux, pai_index; registro cleaner; cleaner.apontador = -1; cleaner.chave = -1; cleaner.idade = -1; cleaner.index_father = -1; strcpy(cleaner.nome, ""); // inicializa lista node *LISTA = (node *) malloc(sizeof (node)); if (!LISTA) { // fprintf(stderr, "Sem memória disponivel!\n"); exit(1); } else { inicia(LISTA); } // arquivo binário if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) { // fprintf(stderr, "\nErro ao abrir arquivo!"); // printf("\nErro ao abrir arquivo!"); return; } // consultar índice a ser removido int indice_remove = consultar(chave, 1); // pode ser 0 até TAMANHO_ARQUIVO if (indice_remove != -1) { // lê o registro da posição a ser removido fseek(arquivo_hashing, ((indice_remove + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); // verifica se corresponde a chave a ser removida if (tabela_h.chave == chave) { // salva índice para pai pai_index = tabela_h.index_father; // salva índice original index_aux = indice_remove; salto = h2(tabela_h.chave) * tabela_h.apontador + index_aux; if (salto >= TAMANHO_ARQUIVO) salto -= TAMANHO_ARQUIVO; while (tabela_h.apontador != -1) { fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade); index_aux = salto; salto = h2(tabela_h.chave) * tabela_h.apontador + salto; if (salto >= TAMANHO_ARQUIVO) salto -= TAMANHO_ARQUIVO; // limpa_registro(index_aux); remove_registro(index_aux); } // remove índice // limpa_registro(indice_remove); remove_registro(indice_remove); limpa_apontador_pai(pai_index); reinsere_lista(LISTA); } } }
/** * Calcula média de acessos as chaves inseridas */ void media_acessos() { float count_jumps = 0, qtd_chaves = 0; // int count_jumps = 0, qtd_chaves = 0; node *LISTA = (node *) malloc(sizeof (node)); if (!LISTA) { exit(1); } else { inicia(LISTA); } if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) { printf("\nErro ao abrir arquivo!"); return; } // int i; // contar as chaves inseridas, salvar em uma lista e consultar até o final da lista for (int i = 0; i < TAMANHO_ARQUIVO; i++) { fseek(arquivo_hashing, ((i + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); // insere todas as chaves do arquivo na lista if (tabela_h.chave != -1) { insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade); qtd_chaves++; } } if (vazia(LISTA)) { // return; } node *tmp; tmp = LISTA->prox; //loop para contar os acessos while (tmp != NULL) { int indice_elemento = consultar(tmp->chave, 1); int exit = 0; // retirar depois // printf("Chave: %d %s %d\n", tmp->chave, tmp->nome, tmp->idade); // consultar todos os pais while (exit == 0) { fseek(arquivo_hashing, (indice_elemento + 1) * sizeof (struct REGISTRO), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); // fprintf(stderr, "jumps %f\n", count_jumps); //saída do loop dos pais if (tabela_h.index_father == -1) { exit = 1; // fprintf(stderr, "cj %d", count_jumps); } else indice_elemento = tabela_h.index_father; // incrementa contador count_jumps++; } // seleciona próximo da lista tmp = tmp->prox; } if (qtd_chaves != 0) { float media_acessos = count_jumps / qtd_chaves; fprintf(stderr, "%.1f", media_acessos); } else { fprintf(stderr, "0.0"); } // media = acessos / TAMANHO_ARQUIVO fclose(arquivo_hashing); }
/** * Inserir registro na tabela hash * @param nome * @param idade * @param chave */ void inserir_chave(int chave, char nome[20], int idade) { int aux, indice = 0, salto, count_jumps = 0, go_to_index, index_aux, index_before_jump; registro register_aux, register_jump; indice = (int) h1(chave); // se já não existir, insere // if (!check_key(chave)) { // não existe if (consultar(chave, 1) == -1) { register_aux.chave = chave; strcpy(register_aux.nome, nome); register_aux.index_father = -1; register_aux.idade = idade; register_aux.apontador = -1; if ((arquivo_hashing = fopen(ARQUIVO, "rb+")) == NULL) { printf("\nErro ao abrir arquivo!"); return; } rewind(arquivo_hashing); fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); // SEM colisão if (tabela_h.chave == -1) { fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); }// else { // verificar se o que está na posição está no seu home adress if (h1(tabela_h.chave) == indice) { // está no homeAdress if (tabela_h.apontador == -1) { //NÃO tem sucessor. Salta para o sucessor salto = h2(tabela_h.chave); fseek(arquivo_hashing, ((salto + indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); index_aux = indice; //salva o índice atual // CASO BASE if (tabela_jump.chave == -1) { // salva o novo registro fseek(arquivo_hashing, ((salto + indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); // salva o indice do pai register_aux.index_father = indice; fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); // lê registro para salvar apontador para sucessor fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); count_jumps++; tabela_h.apontador = count_jumps; // salva apontador para sucessor fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); } // posição ocupada - pular de h2 em h2 até achar uma disponível if (tabela_jump.chave != -1) { // index_aux++; go_to_index = salto + index_aux; // loop de busca de posição vazia do { fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); count_jumps++; if (tabela_jump.chave != -1) { // posição ocupada, procurar nova posição // count_jumps++; //acerta apontador de saltos do home adress // index_aux++; index_aux = go_to_index + salto; // go_to_index = salto + index_aux; go_to_index = go_to_index + salto; if (go_to_index >= TAMANHO_ARQUIVO) go_to_index -= TAMANHO_ARQUIVO; } else { // salva o registro fseek(arquivo_hashing, ((go_to_index + 1) * sizeof (struct REGISTRO)), SEEK_SET); // salva o indice do pai register_aux.index_father = indice; fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); // acertando o apontador de sucessores tabela_h.apontador = count_jumps; //acerta o apontador de saltos fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); } } while (tabela_jump.chave != -1 && go_to_index != indice); } } else { // TEM SUCESSORE(S). É uma cadeia salto = (h2(tabela_h.chave) * tabela_h.apontador) + indice; if (salto >= TAMANHO_ARQUIVO) salto -= TAMANHO_ARQUIVO; // guarda o indice atual para saber onde acertar o apontador int new_index = salto; fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); count_jumps++; while (tabela_jump.apontador != -1 && new_index != indice) { salto = (h2(tabela_jump.chave) * tabela_jump.apontador) + new_index; if (salto >= TAMANHO_ARQUIVO) salto -= TAMANHO_ARQUIVO; new_index = salto; // salva o índice anterior ao salto index_before_jump = new_index; count_jumps++; //salva o número de saltos p/ register_aux fseek(arquivo_hashing, ((new_index + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); } // saiu pq achou o último da cadeia if (new_index != indice) { // salta pelo h2 até achar uma vaga ou terminar o arquivo index_before_jump = new_index; // salva o índice a ser atualizado salto = h2(tabela_jump.chave) + new_index; int index_h2 = h2(tabela_jump.chave); do { fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); if (tabela_jump.chave == -1) { fseek(arquivo_hashing, ((salto + 1) * sizeof (struct REGISTRO)), SEEK_SET); // salva o indice do pai register_aux.index_father = index_before_jump; // register_aux.index_father = indice; fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); // acertar o apontador fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); tabela_jump.apontador = count_jumps; // salva saltos para pai fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); register_aux = tabela_jump; fseek(arquivo_hashing, ((index_before_jump + 1) * sizeof (struct REGISTRO)), SEEK_SET); // fwrite(&tabela_jump, sizeof (struct REGISTRO), 1, arquivo_hashing); fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); // fclose(arquivo_hashing); // previnir loop infinito return; } else { // saltando de h2 em h2 até encontrar uma posição new_index = salto; salto += index_h2; if (salto >= TAMANHO_ARQUIVO) salto -= TAMANHO_ARQUIVO; } count_jumps++; } while (tabela_jump.apontador != -1 || salto != indice); } } }// NÃO ESTÁ NO HOME ADRESS else { // retira ele e os seus sucessores e os reinsere int pai = tabela_h.index_father; // inicializa lista node *LISTA = (node *) malloc(sizeof (node)); if (!LISTA) { // fprintf(stderr, "Sem memória disponivel!\n"); exit(1); } else { inicia(LISTA); } // NÃO TEM SUCESSORES - APENAS 1 A SER REINSERIDO if (tabela_h.apontador == -1) { int key = tabela_h.chave, age = tabela_h.idade; char name[20]; strcpy(name, tabela_h.nome); // salva na lista para reinserção insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade); // insere no lugar deste o registro fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); fclose(arquivo_hashing); // ACERTA APONTADOR DO PAI limpa_apontador_pai(pai); // reinsere o elemento reinserir_chave(key, name, age); // REINICIA A LISTA inicia(LISTA); return; }// tem que reinserir todos até o final da cadeia else { int jump_to; // LOOP até encontrar o final da cadeia while (tabela_h.apontador != -1) { // salva na lista para reinserção insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade); // calcula salto para o próximo para inserir jump_to = (h2(tabela_h.chave) * tabela_h.apontador) + indice; if (jump_to >= TAMANHO_ARQUIVO) jump_to -= TAMANHO_ARQUIVO; // MARCAR POSIÇÃO COMO VÁLIDA limpa_registro(indice); // lê o próximo da cadeia fseek(arquivo_hashing, ((jump_to + 1) * sizeof (struct REGISTRO)), SEEK_SET); fread(&tabela_h, sizeof (struct REGISTRO), 1, arquivo_hashing); } // insere o último da cadeia insereFim(LISTA, tabela_h.chave, tabela_h.nome, tabela_h.idade); // MARCAR POSIÇÃO COMO VÁLIDA limpa_registro(jump_to); // após inserir antigos na lista, insere novo fseek(arquivo_hashing, ((indice + 1) * sizeof (struct REGISTRO)), SEEK_SET); fwrite(®ister_aux, sizeof (struct REGISTRO), 1, arquivo_hashing); fclose(arquivo_hashing); // ACERTA APONTADOR DO PAI limpa_apontador_pai(pai); //reinsere os da lista reinsere_lista(LISTA); // reinicia a lista inicia(LISTA); } // exibe(LISTA); } } fclose(arquivo_hashing); } else { fprintf(stderr, "Chave ja existente\n"); } // imprimir() }