Advertisement
Guest User

llolo

a guest
Apr 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4.  
  5. struct nodo
  6. {
  7.     int numero;
  8.     nodo *next;
  9.     int controllo=0;
  10. };
  11.  
  12. nodo *creanodo()
  13. {
  14.     nodo *p;
  15.     p=(nodo*)malloc(sizeof(nodo));
  16.     cout<<"inserisci numero >";
  17.     cin>>p->numero;
  18.     p->next=NULL;
  19.     return p;
  20. }
  21. /*
  22. void creanodo(nodo *testa)
  23. {
  24.     nodo *app;
  25.     app=(nodo*)malloc(sizeof(nodo));
  26.     cout<<"inserisci numero >";
  27.     cin>>app->numero;
  28.     app->next=testa;
  29.     testa=app;
  30. }*/
  31. nodo *attaccotesta(nodo *testa , nodo *pnew)
  32. {
  33.     pnew->next = testa;
  34.     testa = pnew;
  35.     return testa;
  36. }
  37. void visualizza(nodo *testa)
  38. {
  39.     do
  40.     {
  41.         cout<<testa->numero;
  42.         testa=testa->next;
  43.     }
  44.     while(testa!=NULL);
  45. }
  46. void attaccocoda(nodo *testa,nodo *pnew)
  47. {
  48.     nodo *app;
  49.     app=testa;
  50.     while(app->next!=NULL)
  51.     {
  52.         app=app->next;
  53.     }
  54.     app->next=pnew;
  55. }
  56.  
  57. void attaccoricerca(nodo *testa,nodo *pnew)
  58. {
  59.     int n;
  60.     nodo *app;
  61.     app=testa;
  62.     cout<<"numero da cercare > ";
  63.     cin>>n;
  64.     while(n!=app->next->numero && app->next!=NULL)
  65.     {
  66.         app=app->next;
  67.     }
  68.     pnew->next=app->next;
  69.     app->next=pnew;
  70. }
  71. void cancellacoda(nodo *testa)
  72. {
  73.     nodo *app;
  74.     app=testa;
  75.     while(app->next->next!=NULL)
  76.     {
  77.         app=app->next;
  78.     }
  79.     app->next=NULL;
  80. }
  81. nodo *cancellatesta(nodo *testa)
  82. {
  83.     testa=testa->next;
  84.     return testa;
  85. }
  86.  
  87. void cancellaric(nodo *testa,int n)
  88. {
  89.     nodo *app;
  90.     app=testa;
  91.     while(app->next->numero!=n)
  92.     {
  93.         app=app->next;
  94.     }
  95.     app->next=app->next->next;
  96. }
  97.  
  98. //--------------------------
  99. nodo *ordine(nodo*testa)
  100. {
  101.  
  102.     nodo *app;
  103.     nodo *app2;
  104.     nodo *p;
  105.     nodo *p1;
  106.     app=testa;
  107.     nodo *testa2;
  108.     testa2=(nodo*)malloc(sizeof(nodo));
  109.     testa2->next=NULL;
  110.     testa2->numero=app->numero;
  111.     //nodo *p1=NULL;
  112.    
  113.     /*
  114.         ordinamento personalizzato :
  115.             inserisco in testa2 il valore di testa ,
  116.             poi valore del 2° numero  lo confronto destra e sinistra col valore di
  117.             testa 2 se minore o maggiore ,
  118.             del numero successivo .
  119.             (caso particolare da controllare è valutandoo da inserimento centrale""")
  120.     */
  121.     do
  122.     {
  123.         if( app->next->numero < testa2->numero && app->next!= NULL)
  124.         {
  125.             p=(nodo*)malloc(sizeof(nodo));p1=(nodo*)malloc(sizeof(nodo));
  126.             p->numero=app->next->numero;
  127.             p1=testa2;
  128.             while(testa2->numero > app->next->numero && testa2->next!=NULL )
  129.             {
  130.                 app2=testa2;
  131.                 testa2=testa2->next;
  132.             }
  133.             if(testa2->next == NULL)
  134.             {
  135.                 p->next=testa2->next;
  136.                 testa2->next=p;
  137.             }
  138.             else
  139.             {
  140.                 p->next=app2->next;
  141.                 app2->next=p;
  142.             }
  143.            
  144.            
  145.             testa2=p1;
  146.            
  147.         }
  148.         else if(app->next->numero > testa2->numero )
  149.         {
  150.            
  151.             p=(nodo*)malloc(sizeof(nodo));
  152.             p->next=testa2;
  153.             p->numero=app->next->numero;
  154.             testa2=p;
  155.         }
  156.             app=app->next;
  157.     }while(app->next!=NULL);
  158.    
  159. return testa2;
  160. }
  161. //--------------------------
  162. int main()
  163. {
  164.     nodo *testa= NULL;
  165.     nodo *pnew;nodo *testa2;
  166.     bool controllo = false;
  167.     int anzher;
  168.     do
  169.     {
  170.         cout<<"0. uscire"<<endl;
  171.         cout<<"1. crea nodo"<<endl;
  172.         cout<<"2. attacca in testa"<<endl;
  173.         cout<<"3. attacca in coda"<<endl;
  174.         cout<<"4. visualizza"<<endl;
  175.         cout<<"5. attacca da ricerca"<<endl;
  176.         cout<<"6. cancella in coda"<<endl;
  177.         cout<<"7. cancella in testa"<<endl;
  178.         cout<<"8. cancella ricercato"<<endl;
  179.         cout<<"9. ordine"<<endl;
  180.         cout<<"inserisci cosa vuoi fare >"<<endl;
  181.         cin>>anzher;
  182.         switch(anzher)
  183.         {
  184.             case 0:
  185.                 cout<<"uschh!";
  186.                 break;
  187.             case 1:
  188.                 if(controllo==true)
  189.                 {
  190.                     cout<<"devi attaccarlo!"<<endl;
  191.                 }
  192.                 else
  193.                 {
  194.                     pnew = creanodo();
  195.                     controllo=true;
  196.                 }
  197.                 break;
  198.             case 2:
  199.                 if(controllo==true)
  200.                 {
  201.                     testa=attaccotesta(testa,pnew);
  202.                     controllo = false;
  203.                 }
  204.                 else
  205.                 {
  206.                     cout<<"nulla da attaccare"<<endl;
  207.                 }
  208.                 break;
  209.             case 3:
  210.                 if(controllo==true)
  211.                 {
  212.                     attaccocoda(testa,pnew);
  213.                     controllo = false;
  214.                 }
  215.                 else
  216.                 {
  217.                     cout<<"nulla da attaccare"<<endl;
  218.                 }
  219.                 break;
  220.             case 4:
  221.                 if(controllo==true)
  222.                 {
  223.                 }
  224.                 else
  225.                 {
  226.                     pnew=testa;
  227.                     cout<<"--------------"<<endl;
  228.                     visualizza(pnew);
  229.                     cout<<endl;
  230.                     cout<<"--------------"<<endl;
  231.                     cout<<endl;
  232.                 }
  233.                 break;
  234.             case 5:
  235.                 if(controllo==true)
  236.                 {
  237.                     attaccoricerca(testa,pnew);
  238.                     controllo = false;
  239.                 }
  240.                 else
  241.                 {
  242.                     cout<<"nulla da attaccare"<<endl;
  243.                 }
  244.                 break;
  245.             case 6:
  246.                 cancellacoda(testa);
  247.                 break;
  248.             case 7:
  249.                 testa=cancellatesta(testa);
  250.                 break;
  251.             case 9:
  252.                 pnew=testa;
  253.                 testa2 = ordine(pnew);
  254.                 cout<<"--------------"<<endl;
  255.                 visualizza(testa2);
  256.                 cout<<endl;
  257.                 cout<<"--------------"<<endl;
  258.                 break;
  259.             case 8:
  260.                 int n=0;
  261.                 cout<<"inserisci numero > ";
  262.                 cin>>n;
  263.                 if(testa->numero==n)
  264.                 {
  265.                     testa=cancellatesta(testa);
  266.                 }
  267.                 else
  268.                 {
  269.                     cancellaric(testa,n);
  270.                 }
  271.                 break;
  272.         }
  273.     }while(anzher!=0);
  274.    
  275.    
  276.    
  277.     pnew=testa;
  278.     visualizza(pnew);
  279. return 0;  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement