Advertisement
akanoce

Ex. Libro 11.6_3.cpp

Jan 6th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct nodo{
  5.  
  6. int info;
  7. nodo * next;
  8.  
  9.  
  10. nodo ( int a = 0, nodo* b = 0){
  11.  
  12. info = a;
  13. next = b;
  14.  
  15. }//costruttore
  16.  
  17.  
  18.  
  19. };//nodo
  20.  
  21. nodo* inserisci(){
  22.  
  23. int x;
  24. cout<<"Inserisci un valore da inserire in lista: "; cin>>x; cout<<endl;
  25.  
  26. if(x == -1)
  27. return 0;
  28. else
  29. return new nodo(x,inserisci());
  30.  
  31.  
  32.  
  33. }
  34.  
  35. void stampa(nodo * P){
  36.  
  37. if(!P)
  38. return;
  39. else{
  40.  
  41. cout<<P->info<<" ";
  42. return stampa(P->next);
  43.  
  44. }
  45. }//stampa
  46.  
  47. nodo * alt_mix(nodo *P, nodo *Q){
  48.  
  49. nodo * temp;
  50.  
  51. if( !P || !Q)
  52.     return 0;
  53. else{
  54.  
  55. if(P && Q ){
  56.  
  57. if(!P->next){
  58. P->next = Q;
  59. return 0;
  60. }
  61. temp = Q->next;
  62. Q->next = P->next;
  63. P->next = Q;
  64.  
  65. return  alt_mix(Q->next, temp);
  66.  
  67. }
  68.  
  69.  
  70.  
  71.    
  72.  
  73.  
  74. }//!caso base
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81. }//alt_mix
  82.  
  83.  
  84.  
  85.  
  86. main(){
  87.  
  88. cout<<"Inserisci la prima lista . . ."<<endl;
  89. nodo * prima = inserisci();
  90. cout<<"Inserisci la seconda lista . . . "<<endl;
  91. nodo * seconda = inserisci();
  92.  
  93. nodo * alt = alt_mix(prima,seconda);
  94. stampa(alt);
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. }//main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement