Advertisement
Guest User

Untitled

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