Advertisement
chupeta

Untitled

Nov 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. #include <stdio.h>
  2. struct ptr{
  3. int valor;
  4. struct ptr *prox, *ant;
  5. };
  6. struct ptr *inicio, *inicio2;
  7. void inserir_ordenada(int valor){
  8. struct ptr *aux;
  9. if(inicio==(struct ptr *)NULL){
  10. inicio=(struct ptr *)malloc(sizeof(struct ptr));
  11. inicio->valor=valor;
  12. inicio->prox=(struct ptr *)NULL;
  13. inicio->ant=(struct ptr *)NULL;
  14. }else{
  15. aux=inicio;
  16. if(valor<aux->valor){
  17. aux->ant=(struct ptr *)malloc(sizeof(struct ptr));
  18. aux->ant->prox=aux;
  19. aux->ant->ant=(struct ptr *)NULL;
  20. aux->ant->valor=valor;
  21. aux=aux->ant;
  22. inicio=aux;
  23. }else{
  24. while(valor>aux->valor && aux->prox!=(struct ptr *)NULL){
  25. aux=aux->prox;
  26. }
  27. if(valor<aux->valor){
  28. aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
  29. aux->ant->prox->valor=valor;
  30. aux->ant->prox->ant=aux->ant;
  31. aux->ant->prox->prox=aux;
  32. }else{
  33. aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
  34. aux->prox->valor=valor;
  35. aux->prox->ant=aux;
  36. aux->prox->prox=(struct ptr *)NULL;
  37. aux=aux->prox;
  38. }
  39. }
  40. }
  41. }
  42. void criar_crescente(){
  43. struct ptr *aux;
  44. inicio=(struct ptr *)NULL;
  45. int valor;
  46. printf("Digite o valor:");
  47. scanf("%d", &valor);
  48. while(valor>=0){
  49. inserir_ordenada(valor);
  50. printf("Digite o valor:");
  51. scanf("%d", &valor);
  52. }
  53. }
  54. void inserir_ordenada2(int valor){
  55. struct ptr *aux;
  56. if(inicio2==(struct ptr *)NULL){
  57. inicio2=(struct ptr *)malloc(sizeof(struct ptr));
  58. inicio2->valor=valor;
  59. inicio2->prox=(struct ptr *)NULL;
  60. inicio2->ant=(struct ptr *)NULL;
  61. }else{
  62. aux=inicio2;
  63. if(valor<aux->valor){
  64. aux->ant=(struct ptr *)malloc(sizeof(struct ptr));
  65. aux->ant->prox=aux;
  66. aux->ant->ant=(struct ptr *)NULL;
  67. aux->ant->valor=valor;
  68. aux=aux->ant;
  69. inicio2=aux;
  70. }else{
  71. while(valor>aux->valor && aux->prox!=(struct ptr *)NULL){
  72. aux=aux->prox;
  73. }
  74. if(valor<aux->valor){
  75. aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
  76. aux->ant->prox->valor=valor;
  77. aux->ant->prox->ant=aux->ant;
  78. aux->ant->prox->prox=aux;
  79. }else{
  80. aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
  81. aux->prox->valor=valor;
  82. aux->prox->ant=aux;
  83. aux->prox->prox=(struct ptr *)NULL;
  84. aux=aux->prox;
  85. }
  86. }
  87. }
  88. }
  89. void criar_crescente2(){
  90. struct ptr *aux;
  91. inicio2=(struct ptr *)NULL;
  92. int valor;
  93. printf("Digite o valor:");
  94. scanf("%d", &valor);
  95. while(valor>=0){
  96. inserir_ordenada(valor);
  97. printf("Digite o valor:");
  98. scanf("%d", &valor);
  99. }
  100. }
  101. void juntar_lista(){
  102. struct ptr *aux;
  103. if(inicio2->valor<inicio->valor){
  104. inicio->ant=(struct ptr *)malloc(sizeof(struct ptr));
  105. inicio->ant->valor=inicio2->valor;
  106. inicio->ant->prox=inicio;
  107. inicio->ant->ant=(struct ptr *)NULL;
  108. inicio=inicio->ant;
  109. }else{
  110. aux=inicio;
  111. while(inicio2->valor>aux->valor && aux->prox!=(struct ptr*)NULL)
  112. aux=aux->prox;
  113. if(inicio2->valor<aux->valor){
  114. aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
  115. aux->ant->prox->valor=inicio2->valor;
  116. aux->ant->prox->ant=aux->ant;
  117. aux->ant->prox->prox=aux;
  118. }else{
  119. aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
  120. aux->prox->ant=aux;
  121. aux->prox->valor=inicio2->valor;
  122. aux->prox->prox=(struct ptr *)NULL;
  123. }
  124. }
  125. inicio2=inicio2->prox;
  126. }
  127. void mostra(){
  128. struct ptr *aux;
  129. aux = inicio;
  130. if (inicio==(struct ptr *) NULL)
  131. printf ("Lista vazia!");
  132. else{
  133. printf ("Elementos da lista: ");
  134. while (aux!=(struct ptr *) NULL){
  135. printf ("%d", aux->valor);
  136. if (aux->prox!=(struct ptr *) NULL)
  137. printf ("->");
  138. aux = aux->prox;
  139. }
  140. }
  141. }
  142. main(){
  143. criar_crescente();
  144. criar_crescente2();
  145. juntar_lista();
  146. mostra();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement