Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct nodo{int info; nodo* next; nodo(int a=0, nodo* b=0){info=a; next=b;}};
  4. struct nodoP{nodo* P; nodoP* next; nodoP(nodo* a=0, nodoP* b=0){P=a; next=b;}};
  5.  
  6. nodo*leggi(){
  7. int info;
  8. cin>>info;
  9. //caso base
  10. if(info==-1) return 0;
  11. //caso ricorsivo
  12. else return new nodo(info,leggi());
  13. }
  14.  
  15. //PRE=(Lista(L) è ben formata, x è definito)
  16. nodoP* aux(nodo*L, int x){
  17. //caso base
  18. if(!L) return 0;
  19. //caso ricorsivo
  20. if(L->info==x) return new nodoP(L,aux(L->next,x));
  21. else return aux(L->next,x);
  22. }
  23. //POST=(restituisce una lista di nodoP lunga come il n. di nodi di Lista(L) con x=info e i
  24. //cui nodi puntano a questi nodi di L nell’ordine in cui i nodi sono in L)
  25.  
  26. nodoP*getlast(nodoP*n){
  27. if(!n->next) return n;
  28. else return getlast(n->next);
  29. }
  30.  
  31. nodoP*F(nodo*L){
  32. int x;
  33. cin>>x;
  34. //caso base
  35. if(x==-1) return 0;
  36. //caso ricorsivo
  37. nodoP*aggiunta=aux(L,x);
  38. if(!aggiunta) return F(L);
  39. nodoP*ultimo=getlast(aggiunta);
  40. ultimo->next=F(L);
  41. return aggiunta;
  42. }
  43.  
  44. void stampa(nodoP*y){
  45. if(y){
  46. cout<<y->P->info<<" ";
  47. stampa(y->next);
  48. }
  49. }
  50.  
  51. int main()
  52. {
  53. cout<<"start"<<endl;
  54. nodo*L=leggi();
  55. nodoP*y=F(L);
  56. stampa(y);
  57. cout<<endl;
  58. cout<<"end"<<endl;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement