Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. struct nodo{int info; nodo*next; nodo(int a=0, nodo*b=0){info=a; next=b;}};
  5. // funzioni per input e output
  6. nodo *buildL(int k)
  7. {
  8.   if(!k) return 0;
  9.   else
  10.     {
  11.       int x;
  12.       cin>>x;
  13.       return new nodo(x,buildL(k-1));
  14.     }
  15. }
  16. void leggiA(int*A,int k)
  17. {
  18.   for(int i=0; i<k; i++)
  19.     cin>>A[i];
  20. }
  21. nodo* clone(nodo*L)
  22. {
  23.   if(!L) return 0;
  24.   else
  25.     return new nodo(L->info,clone(L->next));
  26. }
  27. void stampa(nodo*L)
  28. {
  29.   if(!L) cout<<endl;
  30.   else
  31.     {
  32.       cout<<L->info<<' ';
  33.       stampa(L->next);
  34.  
  35.     }
  36. }
  37.  
  38. void concatena(nodo*& L, nodo* L1)
  39. {
  40.     if(!L)
  41.     {
  42.         L = L1;
  43.     }
  44.     else
  45.         concatena(L->next,L1);
  46. }
  47. //L è una lista di nodi ben formata, n è il numero di pezzi che devo staccare
  48. nodo* stacca(nodo*&L,int n)
  49. {
  50.     if(!n || !L)
  51.     {
  52.         nodo* supp;
  53.         supp = L;
  54.         L = 0;
  55.         return supp;
  56.     }
  57.  
  58.     return stacca(L->next, n-1);
  59. }
  60. void Fric(nodo*L, int*A, int dimA, nodo*&L1, nodo*&L2)
  61. {
  62.     if(!L)
  63.         return;
  64.     if(!dimA)
  65.     {
  66.         concatena(L1, L);
  67.         return;
  68.     }
  69.  
  70.     nodo* nL = stacca(L, *A);
  71.     if(dimA%2 == 0)
  72.         concatena(L1, L);
  73.     else
  74.         concatena(L2, L);
  75.     Fric(nL, A+1, dimA-1, L1, L2);
  76. }
  77.  
  78. int main()
  79. {
  80.   cout<<"start"<<endl;
  81.   int n, dimA;
  82.   cin >> n >> dimA;
  83.   int*A=new int[dimA];
  84.   nodo*L=buildL(n);
  85.   stampa(L);
  86.   leggiA(A,dimA);
  87.  
  88.   nodo*L1=0,*L2=0;
  89.   Fric(L,A,dimA,L1,L2);
  90.   stampa(L1);
  91.   stampa(L2);
  92.  
  93.   cout<<"end"<<endl;
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement