Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct nodo{int info; nodo* next; nodo(int a =0, nodo* b=0){info=a; next=b;}};
  5. struct nodoL{nodo* info; nodoL* next; nodoL(nodo* a =0, nodoL* b=0){info=a; next=b;}};
  6.  
  7.  
  8. nodo* clone(nodo*L)
  9. {
  10.   if(L)
  11.     return new nodo(L->info,clone(L->next));
  12.   return 0;
  13. }
  14.  
  15. void stampaL(nodo*L)
  16. {
  17.   if(L)
  18.     {cout<<L->info<<' '; stampaL(L->next);}
  19.   else
  20.     cout<<endl;
  21.  
  22. }
  23. void stampaLL(nodoL*LL,int n)
  24. {
  25.   if(LL)
  26.     {cout<<"fetta "<<n<<") "; stampaL(LL->info); stampaLL(LL->next,n+1);}
  27. }
  28.  
  29. nodo* faiL(int n)
  30. {
  31.   if(n>0)
  32.     return new nodo(n, faiL(n-1));
  33.   return 0;
  34.  
  35. }
  36.  
  37. int contaNodi(nodo*L){
  38.     if(!L) return 0;
  39.     return 1+contaNodi(L->next);
  40. }
  41.  
  42. nodo* split(nodo*&L, int x){
  43.     if(!L) return 0;
  44.     if(x==0) {nodo* tmp=0; return tmp;}
  45.     if(x>contaNodi(L)) return 0; //se il numero di nodi da prelevare รจ maggiore del numero di nodi presenti L resta invariato
  46.     nodo* tmp=L;
  47.     L=L->next;
  48.     tmp->next=split(L, x-1);
  49.    
  50.     return tmp;
  51. }
  52.  
  53. nodoL* affettaric(nodo*&L, int*A, int dimA){
  54.     if(!L) return 0;
  55. //    if(!A) return 0;
  56.     if(!dimA) return 0;
  57.     nodoL* tmp=new nodoL;
  58.     nodo* o=split(L, A[0]);
  59.     tmp->info=o;
  60.     tmp->next=affettaric(L, A+1, dimA-1);
  61.     return tmp;
  62. }
  63.  
  64. main()
  65. {
  66.   cout<<"start"<<endl;
  67.   nodo* L=faiL(10);
  68.   nodo*L0=clone(L);
  69.   int n, A[10];
  70.   cin >> n;
  71.   for(int i=0; i<n; i++)
  72.     cin >> A[i];
  73.   nodoL*K=affettaric(L,A,n);
  74.   cout<<"Lric=";
  75.   stampaL(L);
  76.   cout<<"LLric:"<<endl;
  77.   stampaLL(K,1);
  78.   cout<<endl;
  79.  /* K=affettaiter(L0,A,n);
  80.   cout<<"Liter=";
  81.   stampaL(L0);
  82.   cout<<"LLiter:"<<endl;
  83.   stampaLL(K,1);*/
  84.  
  85.   cout<<"end"<<endl;
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement