Advertisement
Guest User

Untitled

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