Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 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.  
  6. nodo* crea(int dim)
  7. {
  8.     if(dim)
  9.     {
  10.         int x;
  11.         cin>>x;
  12.         return new nodo(x,crea(dim-1));
  13.     }
  14.     return 0;
  15. }
  16.  
  17.  
  18. void leggi(int dim, int*P)
  19. {
  20.     if(dim)
  21.     {
  22.         cin>>*P;
  23.         leggi(dim-1,P+1);
  24.     }
  25.  
  26. }
  27.  
  28. //dice se c'è o no il pattern da n in poi (n incluso)
  29. nodo* find_pattern(nodo* nodo, int *P, int dimP)
  30. {
  31.  
  32.  
  33.     if (nodo != NULL && nodo->info == P[0])
  34.     {
  35.         if (dimP > 1)
  36.         {
  37.             return find_pattern(nodo->next, P + 1, dimP - 1);
  38.         }
  39.         else return nodo;
  40.     }
  41.     else
  42.     {
  43.         return NULL;
  44.     }
  45. }
  46.  
  47. //PRE=(L(n) è una lista corretta, P ha dimP elementi, chiamiamo vL(n)=L(n))
  48. nodo* match(nodo* &n, int*P, int dimP)
  49. {
  50.     nodo *lastInPattern = find_pattern(n, P, dimP);
  51.     if (lastInPattern != NULL)
  52.     {
  53.         nodo *pattern = n; //salvo n in pattern perchè n è il primo nodo del pattern
  54.         n = lastInPattern->next; //n è un nodo del pattern quindi lo cambio mettendolo uguale al nodo che succede l'ultimo nodo del pattern
  55.         lastInPattern->next = NULL; //il successivo dell'ultimo nodo del pattern deve essere NULL o conterrebbe anche nodi che non fanno parte
  56.  
  57.         return pattern;
  58.     }
  59.     else
  60.     {
  61.         if(n->next != NULL) return match(n->next, P, dimP);
  62.         else return NULL;
  63.     }
  64. }
  65. //POST=(in L(n) c’è un match di P, allora la funzione restituisce col return match(vL(n),P[0..dimP-1]) e
  66. //L(n)=resto_mach(vL(n),P[0..dimP-1], se invece non c’è il match allora la funzione restituisce 0 e L(n)=vL(n))
  67.  
  68. void stampa(nodo *n)
  69. {
  70.    
  71.     if (n != NULL)
  72.     {
  73.         cout << n->info << " ";
  74.         stampa(n->next);
  75.     }
  76.     else
  77.     {
  78.         cout << endl;
  79.     }
  80. }
  81.  
  82. main()
  83. {
  84.     int dim, dimP, P[20];
  85.     cout<<"start"<<endl;
  86.     cin>>dim;
  87.     nodo* L1=crea(dim);
  88.     cin>>dimP;
  89.     leggi(dimP, P);
  90.     nodo* L2= match(L1,P, dimP);//da fare
  91.     stampa(L2); //da fare
  92.     stampa(L1);
  93.     cout<<"end"<<endl;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement