Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. /*
  2. Dadas dos listas LISTA y LISTB (nodo = registro + puntero), desarrollar y codificar un
  3. procedimiento que genere otra lista LISTC por apareo del campo LEGAJO del registro
  4. (define orden creciente en ambas). Nota: LISTA y LISTB dejan de ser útiles después del
  5. apareo).
  6. */
  7.  
  8. #include <iostream>
  9. #include <stdlib.h>
  10. #include <time.h>
  11.  
  12. using namespace std;
  13.  
  14. struct Nodo{
  15. int info;
  16. Nodo* sgte;
  17. };
  18.  
  19. Nodo* InsertarPrimero(Nodo* &lista, int valor){
  20. Nodo *p = new Nodo();
  21. p->info = valor;
  22. p->sgte = lista;
  23. lista = p;
  24. return p;
  25. }
  26.  
  27. Nodo* InsertarEnMedio(Nodo* &lista, int valor){
  28. Nodo* q = new Nodo();
  29. Nodo* p = lista;
  30. q->info = valor;
  31. while (p->sgte != NULL && valor > p->sgte->info){
  32. p = p->sgte;
  33. }
  34. q->sgte = p->sgte;
  35. p->sgte = q;
  36. return q;
  37. }
  38.  
  39. Nodo* InsertarOrdenado(Nodo* &lista, int valor){
  40. Nodo* nuevo = NULL;
  41. if (lista == NULL || valor < lista->info) {
  42. nuevo = InsertarPrimero(lista, valor);
  43. } else{
  44. nuevo = InsertarEnMedio(lista, valor);
  45. }
  46. return nuevo;
  47. }
  48.  
  49. void LlenarListasOrdenado(Nodo* &lista, int desde, int hasta, int incremento){
  50.  
  51. for (int i = desde; i < hasta; i+= incremento) {
  52. InsertarOrdenado(lista, i);
  53. }
  54. }
  55.  
  56. Nodo* AparearListas(Nodo* &listaA, Nodo* &listaB){
  57.  
  58. Nodo* A = listaA;
  59. Nodo* B = listaB;
  60. Nodo* listaAB = NULL;
  61.  
  62. while(A != NULL && B != NULL){
  63. if(A->info < B->info){
  64. InsertarOrdenado(listaAB, A->info);
  65. A = A->sgte;
  66. }else{
  67. InsertarOrdenado(listaAB, B->info);
  68. B = B->sgte;
  69. }
  70. }
  71.  
  72. while(A != NULL){
  73. InsertarOrdenado(listaAB, A->info);
  74. A = A->sgte;
  75. }
  76.  
  77. while(B != NULL){
  78. InsertarOrdenado(listaAB, B->info);
  79. B = B->sgte;
  80. }
  81.  
  82.  
  83. return listaAB;
  84.  
  85. }
  86.  
  87. void MostrarLista(Nodo* &lista){
  88. Nodo* p = lista;
  89. while (p != NULL){
  90. cout << p->info << endl;
  91. p = p->sgte;
  92. }
  93. }
  94.  
  95. int main() {
  96. //Inicializo variables
  97. Nodo* listaA = NULL;
  98. Nodo* listaB = NULL;
  99. Nodo* listaAB = NULL;
  100. srand( time(NULL) );
  101.  
  102. //Lleno las listas con valores en orden
  103. LlenarListasOrdenado(listaA,0,10,2);
  104. LlenarListasOrdenado(listaB,1,10,2);
  105.  
  106. //Apareo las listas
  107. listaAB = AparearListas(listaA, listaB);
  108.  
  109. //Muestro la lista final
  110. MostrarLista(listaAB);
  111.  
  112. return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement