Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. void avl_max_subarvore(arvore_avl *arv, const char *inicio) {
  2. /* prob 1.1 - a implementar */
  3.  
  4. if ((arv || inicio) == NULL)
  5. return;
  6.  
  7. no_avl *raizsub;
  8.  
  9. raizsub = avl_pesquisa(arv, inicio);
  10.  
  11. while ((raizsub->direita) != NULL) {
  12. raizsub = raizsub->direita;
  13. }
  14.  
  15. printf("%s", raizsub->str);
  16.  
  17. }
  18.  
  19. tabela_dispersao* tabela_copia(tabela_dispersao *original, int novotamanho) {
  20. /* prob 1.2 - a implementar */
  21.  
  22. if ((original == NULL) || (novotamanho == NULL) || (novotamanho == 0))
  23. return NULL;
  24. int i;
  25. tabela_dispersao *tabnova;
  26. tabnova = tabela_nova(novotamanho, original->hfunc);
  27.  
  28. for (i = 0; i < original->tamanho; i++) {
  29. if (original->elementos[i]) {
  30. tabela_adiciona(tabnova, original->elementos[i]->obj->chave,
  31. original->elementos[i]->obj->valor);
  32.  
  33. }
  34. }
  35.  
  36. return tabnova;
  37. }
  38.  
  39. int proximas_n_chegadas(lista *tempos, lista *origens, lista *aeroportos, int n) {
  40. /* prob 2.1 - a implementar */
  41.  
  42. if (tempos == NULL || origens == NULL || aeroportos == NULL)
  43. return 0;
  44. if (n == 0)
  45. return 0;
  46.  
  47. int i, prioridade, j, k, indice;
  48. heap *hp;
  49. hp = heap_nova(25);
  50. elemento *tempo, *aux, *aux3;
  51. tempo = tempos->inicio;
  52. aux = origens->inicio;
  53. aux3 = aeroportos->inicio;
  54. char *aux2;
  55.  
  56. for (i = 0; i < lista_tamanho(tempos); i++) {
  57.  
  58. prioridade = atoi(tempo->str);
  59. tempo = tempo->proximo;
  60.  
  61. heap_insere(hp, aux->str, prioridade);
  62. aux = aux->proximo;
  63. }
  64.  
  65. // mostraHeap(hp, 0); // hp tem string de ints com cod do aeroporto e está organizada com prioridade de menor tempo
  66.  
  67. for (j = 0; j < n; j++) {
  68.  
  69. aux2 = heap_remove(hp);
  70.  
  71. indice = atoi(aux2);
  72. aux3 = aeroportos->inicio;
  73.  
  74. for (k = 0; k < indice; k++)
  75. aux3 = aux3->proximo;
  76.  
  77. printf("%i : %s\n", j + 1, aux3->str);
  78. }
  79.  
  80. return 1;
  81. }
  82.  
  83. lista* pesquisa_destinos(grafo *rotas, lista *aeroportos, const char *origem) {
  84. /* prob 2.2 - a implementar */
  85.  
  86. if (rotas == NULL || origem == NULL || aeroportos == NULL)
  87. return NULL;
  88.  
  89. int i, k, l, j, z;
  90. elemento *aux, *aux2;
  91. aux = aeroportos->inicio;
  92. vetor *vec;
  93. vec = vetor_novo();
  94. lista *ls;
  95. ls = lista_nova();
  96.  
  97. for (i = 0; i < lista_tamanho(aeroportos); i++) {
  98. if (strcmp(origem, aux->str) == 0) //encontrou o aeroporto
  99. break;
  100. aux = aux->proximo;
  101. }
  102.  
  103. //Encontrou o indice de Lille
  104. //printf("%i\n", i);
  105.  
  106. vec = v_sucessores(rotas, i);
  107. //printf("%i\n", vetor_tamanho(vec));
  108.  
  109. for (k = 0; k < vetor_tamanho(vec); k++) {
  110.  
  111. aux = aeroportos->inicio;
  112. j = vec->elementos[k].val;
  113.  
  114. for (l = 0; l < j; l++) {
  115. aux = aux->proximo;
  116. }
  117.  
  118. lista_insere(ls, aux->str, NULL);
  119. }
  120.  
  121. z = lista_ordena(ls);
  122.  
  123. if (z == 0)
  124. return ls;
  125.  
  126. else
  127. NULL;
  128. }
  129.  
  130. ------------------- Teste exemplo 2
  131.  
  132. void avl_imprime_ord(arvore_avl* avl, int ordem) {
  133. /* prob 1.1 - a implementar */
  134.  
  135. no_avl *aux;
  136.  
  137. //CRESCENTE
  138. if (ordem >= 0) {
  139.  
  140. while (avl->raiz != NULL) {
  141.  
  142. aux = avl->raiz;
  143.  
  144. while (aux->esquerda != NULL)
  145. aux = aux->esquerda;
  146.  
  147. printf("%s ", aux->str);
  148.  
  149. avl_remove(avl, aux->str);
  150. }
  151. }
  152.  
  153. //DECRESCENTE
  154. else if (ordem >= 0) {
  155.  
  156. while (avl->raiz != NULL) {
  157.  
  158. aux = avl->raiz;
  159.  
  160. while (aux->direita != NULL)
  161. aux = aux->direita;
  162.  
  163. printf("%s ", aux->str);
  164.  
  165. avl_remove(avl, aux->str);
  166. }
  167. }
  168.  
  169. }
  170.  
  171. void valida_passwds(FILE *f, lista *login, lista *passwd) {
  172. /* prob 1.2 - a implementar */
  173.  
  174. tabela_dispersao *td;
  175. td = tabela_nova(10, hash_djbm);
  176. const char delimitador1[] = " ", delimitador2[] = "\n";
  177. char string[50];
  178. char *user, *pw;
  179. int i;
  180.  
  181. while ((fgets(string, 50, f)) != NULL) {
  182. string[strlen(string) - 1] = '\0';
  183.  
  184. user = strtok(string, delimitador1);
  185. pw = strtok(NULL, delimitador2);
  186.  
  187. tabela_adiciona(td, user, pw);
  188.  
  189. }
  190.  
  191. //mostraTabela(td);
  192. elemento *aux1, *aux2;
  193. aux1 = login->inicio;
  194. aux2 = passwd->inicio;
  195. char *aux3;
  196. int z;
  197.  
  198. for (i = 0; i < (login->tamanho); i++) {
  199.  
  200. printf("Login: %s\n", aux1->str);
  201. printf("Password: %s\n", aux2->str);
  202.  
  203. aux3 = tabela_valor(td, aux1->str);
  204.  
  205. z = tabela_existe(td, aux2->str);
  206.  
  207. if (z == 0) {
  208. printf("Sucesso\n");
  209. }
  210.  
  211. else
  212. printf("Insucesso\n");
  213.  
  214. aux1 = aux1->proximo;
  215. aux2 = aux2->proximo;
  216. }
  217.  
  218. }
  219.  
  220. lista* descobre_caminho(grafo *g, int origem, int destino, int obrigatorio) {
  221. /* prob 2.1 - a implementar */
  222.  
  223. int *parte1, *parte2;
  224. int tamcam1, tamcam2, i;
  225. lista *ls;
  226. ls = lista_nova();
  227. char str[2];
  228.  
  229. parte1 = grafo_bfs(g, origem, obrigatorio, &tamcam1);
  230. if (parte1 == 0 || parte1 == NULL)
  231. return NULL;
  232.  
  233. parte2 = grafo_bfs(g, obrigatorio, destino, &tamcam2);
  234. if (parte2 == 0 || parte2 == NULL)
  235. return NULL;
  236.  
  237. for (i = 0; i < tamcam1; i++) {
  238. sprintf(str, "%i", *(parte1 + i));
  239. lista_insere(ls, str, -1);
  240. }
  241.  
  242. for (i = 0; i < tamcam2 - 1; i++) {
  243. sprintf(str, "%i", *(parte2 + i));
  244. lista_insere(ls, str, -1);
  245. }
  246.  
  247. return ls;
  248. }
  249.  
  250. int simula_acontecimentos(lista *acoes, lista *tempos, int n) {
  251. /* prob 2.2 - a implementar */
  252.  
  253. heap *hp;
  254. hp = heap_nova(20);
  255. int i, j;
  256. elemento_lista *aux1, *aux2;
  257. aux1 = acoes->raiz;
  258. aux2 = tempos->raiz;
  259. char *str;
  260.  
  261. for (i = 0; i < lista_tamanho(tempos); i++) {
  262.  
  263. heap_insere(hp, aux1->str, (atoi(aux2->str)));
  264.  
  265. aux1 = aux1->proximo;
  266. aux2 = aux2->proximo;
  267. }
  268.  
  269. for (j = 1; j <= n; j++) {
  270. str = heap_remove(hp);
  271. printf("%i : %s\n", j, str);
  272. }
  273.  
  274. return 1;
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement