Advertisement
richard1992

Untitled

Jul 22nd, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. //Richard Andrei Aquino dos Santos
  2. //AED2
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. typedef struct dado {
  10. char valor;
  11. struct dado *prox;
  12. } Hash;
  13.  
  14. void insereNovo(Hash **tabela, int i, char letra);
  15. void insereHash(Hash** tabela, char valorA, char valorB, int tamanho);
  16. void ordena(Hash** tabela, int i);
  17. int tamanho;
  18.  
  19. int main() {
  20. int nTestes, C, k, i, j;
  21. char letra, auxletra = 'a', a = 'a', b = 'a';
  22. Hash** tabela;
  23. Hash* tmp;
  24. scanf("%d", &nTestes); //nTestes serve para fazer varios testes de hashs de entradas diferentes
  25. tabela = (Hash **)malloc(sizeof(Hash*));
  26.  
  27. for (k = 0; k < nTestes; k++) {
  28. letra = 'a';
  29. scanf("%d %d", &tamanho, &C); //tamanho = tamanho do vertice , C numero de arestas
  30. tabela = (Hash **)realloc(tabela, tamanho * sizeof(Hash*)); // aloca tabela com tamanho inicial maximo
  31.  
  32. for (i = 0; i < tamanho; i++) { // insere todos as letras usadas em cada posição inicial na tabela
  33. insereNovo(tabela, i, letra);
  34. letra++;//a +1 = b // b + 1 = c
  35. }
  36. for (i = 0; i < tamanho; i++) {
  37. printf("%d %c\n", i, tabela[i]->valor);//testando se inserções foram corretas
  38. }
  39.  
  40. for (i = 0; i < C; i++) { // recebendo valores para serem inseridos
  41. scanf("%c %c", &a, &b);
  42. insereHash(tabela, a, b, tamanho);
  43. }
  44. j = 0;
  45. while (j < tamanho) { // j = indica a chave
  46. printf("Case #%d:\n", j); // imprime chave no momento
  47. tmp = tabela[j];
  48. while (tmp != NULL) { //Enquanto existir algo
  49. printf("%c,", tmp->valor); //imprime o valor e
  50. tmp = tmp->prox; // avança pro proximo
  51. }
  52. printf("\n");
  53. j++;
  54. }
  55. }
  56. free(tabela);
  57. return 0;
  58. }
  59.  
  60. void insereHash(Hash** tabela, char valorA, char valorB, int tamanho) {
  61. Hash *novoA, *novoB, *auxA, *auxB;
  62. int i, matchA = 0, matchB = 0, aux = 0, ia = 1000, ib = 1000, iaux;
  63.  
  64. for (i = 0; i < tamanho; i++) {
  65. novoA = tabela[i];
  66. novoB = tabela[i];
  67. aux = 0;
  68. if (matchA == 0) { // matchA indica quando foi encontrado na tabela o valorA recebido
  69. while (aux == 0) { //aux indica se foi encontrado ou chegou em NULL
  70. if (novoA->valor == valorA) {
  71. matchA = 1;
  72. aux = 1;
  73. ia = i;
  74. }
  75. else {
  76. novoA = novoA->prox;
  77. }
  78. if (novoA == NULL) {//não esta nessa lista então vamos pra proxima
  79. aux = 1;
  80. }
  81.  
  82. }
  83. }
  84. aux = 0;
  85. if (matchB == 0) {// matchB indica quando foi encontrado na tabela o valorB recebido
  86. while (aux == 0) {//aux indica se foi encontrado ou chegou em NULL
  87. if (novoB->valor == valorB) {
  88. matchB = 1;
  89. aux = 1;
  90. ib = i;
  91. }
  92. else {
  93. novoB = novoB->prox;
  94. }
  95. if (novoB == NULL) {
  96. aux = 1;
  97. }
  98.  
  99. }
  100. }
  101.  
  102. }
  103. aux = 0;
  104. novoA = tabela[ia]; //ia aponta em qual lista foi encontrado o valorA
  105. novoB = tabela[ib]; //ib aponta em qual lista foi encontrado o valorB
  106. if (ia < ib) {// Se a lista os valores da lista A são menores que da lista B
  107. while (novoA->prox != NULL)
  108. novoA = novoA->prox;
  109. novoA->prox = novoB; // coloca a lista ja encontrada em novoB no fim da lista de novoA
  110. ordena(tabela, ia);// ordena a nova lista formada por A e B
  111. tabela[ib] = NULL;//diz que a tabela onde a lista B estava não será mais utilizada
  112. }
  113. else {
  114. if (ia > ib) {// Se a lista os valores da lista B são menores que da lista A
  115. while (novoB->prox != NULL)
  116. novoB = novoB->prox;
  117. novoB->prox = novoA;// coloca a lista ja encontrada em novoA no fim da lista de novoB
  118. ordena(tabela, ib);// ordena a nova lista formada por A e B
  119. tabela[ia] = NULL;//diz que a tabela onde a lista A estava não será mais utilizada
  120. }
  121.  
  122.  
  123. }
  124.  
  125. }
  126.  
  127. void ordena(Hash** tabela, int i){// ordena a lista
  128. Hash *A,*B,*aux;
  129. A = tabela[i];
  130. while (A!= NULL) {
  131. B = A->prox;
  132. while (B != NULL) {
  133. if (A->valor > B->valor) {
  134. aux = A;
  135. A = B;
  136. B = aux;
  137. }
  138. B = B->prox;
  139. }
  140. A = A->prox;
  141. }
  142. }
  143.  
  144. void insereNovo(Hash **tabela, int i, char letra) {
  145. Hash *tmp;
  146. tmp = (Hash *)malloc(sizeof(Hash));
  147. tmp->valor = letra;
  148. tmp->prox = NULL;
  149. tabela[i] = tmp;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement