Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct nodo{int info; nodo* next; nodo(int a=0, nodo* b=0){info=a; next=b;}};
- nodo* crea(int dim)
- {
- if(dim)
- {
- int x;
- cin>>x;
- return new nodo(x,crea(dim-1));
- }
- return 0;
- }
- void leggi(int dim, int*P)
- {
- if(dim)
- {
- cin>>*P;
- leggi(dim-1,P+1);
- }
- }
- nodo* patter_after_node(nodo* nodo, int *P, int dimP)
- {
- if (nodo != NULL && nodo->info == P[0])
- {
- if (dimP > 1)
- {
- return patter_after_node(nodo->next, P + 1, dimP - 1);
- }
- else return nodo;
- }
- else
- {
- return NULL;
- }
- }
- //PRE=(L(n) è una lista corretta, P ha dimP elementi, chiamiamo vL(n)=L(n))
- nodo* match(nodo* &n, int*P, int dimP)
- {
- nodo *lastInPattern = patter_after_node(n, P, dimP);
- if (lastInPattern != NULL)
- {
- nodo *pattern = n; //salvo n in pattern perchè n è il primo nodo del pattern
- n = lastInPattern->next; //n è un nodo del pattern quindi lo cambio mettendolo uguale al nodo che succede l'ultimo nodo del pattern
- lastInPattern->next = NULL; //il successivo dell'ultimo nodo del pattern deve essere NULL o conterrebbe anche nodi che non fanno parte
- return pattern;
- }
- else
- {
- if(n->next != NULL) return match(n->next, P, dimP);
- else return NULL;
- }
- }
- //POST=(in L(n) c’è un match di P, allora la funzione restituisce col return match(vL(n),P[0..dimP-1]) e
- //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))
- void stampa(nodo *n)
- {
- if (n != NULL)
- {
- cout << n->info << " ";
- stampa(n->next);
- }
- else
- {
- cout << endl;
- }
- }
- main()
- {
- int dim, dimP, P[20];
- cout<<"start"<<endl;
- cin>>dim;
- nodo* L1=crea(dim);
- cin>>dimP;
- leggi(dimP, P);
- nodo* L2= match(L1,P, dimP);//da fare
- stampa(L2); //da fare
- stampa(L1);
- cout<<"end"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement