Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- struct nodo
- {
- int numero;
- nodo *next;
- int controllo=0;
- };
- nodo *creanodo()
- {
- nodo *p;
- p=(nodo*)malloc(sizeof(nodo));
- cout<<"inserisci numero >";
- cin>>p->numero;
- p->next=NULL;
- return p;
- }
- /*
- void creanodo(nodo *testa)
- {
- nodo *app;
- app=(nodo*)malloc(sizeof(nodo));
- cout<<"inserisci numero >";
- cin>>app->numero;
- app->next=testa;
- testa=app;
- }*/
- nodo *attaccotesta(nodo *testa , nodo *pnew)
- {
- pnew->next = testa;
- testa = pnew;
- return testa;
- }
- void visualizza(nodo *testa)
- {
- do
- {
- cout<<testa->numero;
- testa=testa->next;
- }
- while(testa!=NULL);
- }
- void attaccocoda(nodo *testa,nodo *pnew)
- {
- nodo *app;
- app=testa;
- while(app->next!=NULL)
- {
- app=app->next;
- }
- app->next=pnew;
- }
- void attaccoricerca(nodo *testa,nodo *pnew)
- {
- int n;
- nodo *app;
- app=testa;
- cout<<"numero da cercare > ";
- cin>>n;
- while(n!=app->next->numero && app->next!=NULL)
- {
- app=app->next;
- }
- pnew->next=app->next;
- app->next=pnew;
- }
- void cancellacoda(nodo *testa)
- {
- nodo *app;
- app=testa;
- while(app->next->next!=NULL)
- {
- app=app->next;
- }
- app->next=NULL;
- }
- nodo *cancellatesta(nodo *testa)
- {
- testa=testa->next;
- return testa;
- }
- void cancellaric(nodo *testa,int n)
- {
- nodo *app;
- app=testa;
- while(app->next->numero!=n)
- {
- app=app->next;
- }
- app->next=app->next->next;
- }
- //--------------------------
- nodo *ordine(nodo*testa)
- {
- nodo *app;
- nodo *app2;
- nodo *p;
- nodo *p1;
- app=testa;
- nodo *testa2;
- testa2=(nodo*)malloc(sizeof(nodo));
- testa2->next=NULL;
- testa2->numero=app->numero;
- //nodo *p1=NULL;
- /*
- ordinamento personalizzato :
- inserisco in testa2 il valore di testa ,
- poi valore del 2° numero lo confronto destra e sinistra col valore di
- testa 2 se minore o maggiore ,
- del numero successivo .
- (caso particolare da controllare è valutandoo da inserimento centrale""")
- */
- do
- {
- if( app->next->numero < testa2->numero && app->next!= NULL)
- {
- p=(nodo*)malloc(sizeof(nodo));p1=(nodo*)malloc(sizeof(nodo));
- p->numero=app->next->numero;
- p1=testa2;
- while(testa2->numero > app->next->numero && testa2->next!=NULL )
- {
- app2=testa2;
- testa2=testa2->next;
- }
- if(testa2->next == NULL)
- {
- p->next=testa2->next;
- testa2->next=p;
- }
- else
- {
- p->next=app2->next;
- app2->next=p;
- }
- testa2=p1;
- }
- else if(app->next->numero > testa2->numero )
- {
- p=(nodo*)malloc(sizeof(nodo));
- p->next=testa2;
- p->numero=app->next->numero;
- testa2=p;
- }
- app=app->next;
- }while(app->next!=NULL);
- return testa2;
- }
- //--------------------------
- int main()
- {
- nodo *testa= NULL;
- nodo *pnew;nodo *testa2;
- bool controllo = false;
- int anzher;
- do
- {
- cout<<"0. uscire"<<endl;
- cout<<"1. crea nodo"<<endl;
- cout<<"2. attacca in testa"<<endl;
- cout<<"3. attacca in coda"<<endl;
- cout<<"4. visualizza"<<endl;
- cout<<"5. attacca da ricerca"<<endl;
- cout<<"6. cancella in coda"<<endl;
- cout<<"7. cancella in testa"<<endl;
- cout<<"8. cancella ricercato"<<endl;
- cout<<"9. ordine"<<endl;
- cout<<"inserisci cosa vuoi fare >"<<endl;
- cin>>anzher;
- switch(anzher)
- {
- case 0:
- cout<<"uschh!";
- break;
- case 1:
- if(controllo==true)
- {
- cout<<"devi attaccarlo!"<<endl;
- }
- else
- {
- pnew = creanodo();
- controllo=true;
- }
- break;
- case 2:
- if(controllo==true)
- {
- testa=attaccotesta(testa,pnew);
- controllo = false;
- }
- else
- {
- cout<<"nulla da attaccare"<<endl;
- }
- break;
- case 3:
- if(controllo==true)
- {
- attaccocoda(testa,pnew);
- controllo = false;
- }
- else
- {
- cout<<"nulla da attaccare"<<endl;
- }
- break;
- case 4:
- if(controllo==true)
- {
- }
- else
- {
- pnew=testa;
- cout<<"--------------"<<endl;
- visualizza(pnew);
- cout<<endl;
- cout<<"--------------"<<endl;
- cout<<endl;
- }
- break;
- case 5:
- if(controllo==true)
- {
- attaccoricerca(testa,pnew);
- controllo = false;
- }
- else
- {
- cout<<"nulla da attaccare"<<endl;
- }
- break;
- case 6:
- cancellacoda(testa);
- break;
- case 7:
- testa=cancellatesta(testa);
- break;
- case 9:
- pnew=testa;
- testa2 = ordine(pnew);
- cout<<"--------------"<<endl;
- visualizza(testa2);
- cout<<endl;
- cout<<"--------------"<<endl;
- break;
- case 8:
- int n=0;
- cout<<"inserisci numero > ";
- cin>>n;
- if(testa->numero==n)
- {
- testa=cancellatesta(testa);
- }
- else
- {
- cancellaric(testa,n);
- }
- break;
- }
- }while(anzher!=0);
- pnew=testa;
- visualizza(pnew);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement