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;}};
- struct nodoP{nodo* P; nodoP* next; nodoP(nodo* a=0, nodoP* b=0){P=a; next=b;}};
- nodo*leggi(){
- int info;
- cin>>info;
- //caso base
- if(info==-1) return 0;
- //caso ricorsivo
- else return new nodo(info,leggi());
- }
- //PRE=(Lista(L) è ben formata, x è definito)
- nodoP* aux(nodo*L, int x){
- //caso base
- if(!L) return 0;
- //caso ricorsivo
- if(L->info==x) return new nodoP(L,aux(L->next,x));
- else return aux(L->next,x);
- }
- //POST=(restituisce una lista di nodoP lunga come il n. di nodi di Lista(L) con x=info e i
- //cui nodi puntano a questi nodi di L nell’ordine in cui i nodi sono in L)
- nodoP*getlast(nodoP*n){
- if(!n->next) return n;
- else return getlast(n->next);
- }
- nodoP*F(nodo*L){
- int x;
- cin>>x;
- //caso base
- if(x==-1) return 0;
- //caso ricorsivo
- nodoP*aggiunta=aux(L,x);
- if(!aggiunta) return F(L);
- nodoP*ultimo=getlast(aggiunta);
- ultimo->next=F(L);
- return aggiunta;
- }
- void stampa(nodoP*y){
- if(y){
- cout<<y->P->info<<" ";
- stampa(y->next);
- }
- }
- int main()
- {
- cout<<"start"<<endl;
- nodo*L=leggi();
- nodoP*y=F(L);
- stampa(y);
- cout<<endl;
- cout<<"end"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement