|
| 1 | +//Soluzione di Filippo Zonta Rocha e Marco Marchesin,sarà aggiornato con commento. |
| 2 | +#include<iostream> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +struct nodo{int info; nodo* next; nodo(int a =0, nodo* b=0){info=a; next=b;}}; |
| 6 | +struct nodoL{nodo* info; nodoL* next; nodoL(nodo* a =0, nodoL* b=0){info=a; next=b;}}; |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +nodo* clone(nodo*L) |
| 11 | +{ |
| 12 | + if(L) |
| 13 | + return new nodo(L->info,clone(L->next)); |
| 14 | + return 0; |
| 15 | +} |
| 16 | + |
| 17 | +void stampaL(nodo*L) |
| 18 | +{ |
| 19 | + if(L) |
| 20 | + {cout<<L->info<<' '; stampaL(L->next);} |
| 21 | + else |
| 22 | + cout<<endl; |
| 23 | + |
| 24 | +} |
| 25 | +void stampaLL(nodoL*LL,int n) |
| 26 | +{ |
| 27 | + if(LL) |
| 28 | + {cout<<"fetta "<<n<<") "; stampaL(LL->info); stampaLL(LL->next,n+1);} |
| 29 | +} |
| 30 | + |
| 31 | +nodo* faiL(int n) |
| 32 | +{ |
| 33 | + if(n>0) |
| 34 | + return new nodo(n, faiL(n-1)); |
| 35 | + return 0; |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +nodo* avanti(nodo*& L, int x){ |
| 40 | + if(!L || x<1) |
| 41 | + return 0; |
| 42 | + // qui L esiste e x > 0 |
| 43 | + nodo* t=L; |
| 44 | + L=L->next; |
| 45 | + t->next=avanti(L,x-1); |
| 46 | + return t; |
| 47 | +} |
| 48 | + |
| 49 | +nodoL* affettaRIC(nodo* &L, int *A, int dimA){ |
| 50 | + if(!L || dimA<1) return 0; |
| 51 | + return new nodoL(avanti (L, *A), |
| 52 | + affettaRIC(L, ++A, --dimA)); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +nodo *avantiiter(nodo*& L, int x){ |
| 57 | + if (!x) |
| 58 | + return 0; |
| 59 | + nodo* ret =L; |
| 60 | + for(int i=0;i<x-1; i++) L=L->next; |
| 61 | + nodo *t = L; |
| 62 | + L=L->next; |
| 63 | + t->next=0; |
| 64 | + return ret; |
| 65 | +} |
| 66 | + |
| 67 | +void concatena (nodoL *&LL, nodoL *b) { |
| 68 | + if(!LL) LL = b; |
| 69 | + else { |
| 70 | + nodoL *c=LL; |
| 71 | + while (c->next) c=c->next; |
| 72 | + c->next=b; |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +nodoL * affettaITER(nodo* &L,int *A, int dimA){ |
| 77 | + nodoL* res=0; |
| 78 | + for(int j=0;j<dimA;j++) { |
| 79 | + concatena(res, new nodoL(avantiiter(L,A[j]), NULL)); |
| 80 | + } |
| 81 | + return res; |
| 82 | +} |
| 83 | + |
| 84 | +main() |
| 85 | +{ |
| 86 | + cout<<"start"<<endl; |
| 87 | + nodo* L= faiL(10); |
| 88 | + nodo*L0=clone(L); |
| 89 | + int n, A[10]; |
| 90 | + cin >> n; |
| 91 | + for(int i=0; i<n; i++) |
| 92 | + cin >> A[i]; |
| 93 | + nodoL*K=affettaRIC(L,A,n); |
| 94 | + cout<<"Lric="; |
| 95 | + stampaL(L); |
| 96 | + cout<<"LLric:"<<endl; |
| 97 | + stampaLL(K,1); |
| 98 | + cout<<endl; |
| 99 | + K=affettaITER(L0,A,n); |
| 100 | + cout<<"Liter="; |
| 101 | + stampaL(L0); |
| 102 | + cout<<"LLiter:"<<endl; |
| 103 | + stampaLL(K,1); |
| 104 | + |
| 105 | + cout<<"end"<<endl; |
| 106 | + |
| 107 | +} |
0 commit comments