Advertisement
Guest User

Untitled

a guest
May 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. //creare una lista di 3 elementi ad esempio
  5.  
  6. struct nodo{int info, nodo*next;
  7. nodo(int a=0,nodo*b=0){info=a,next=b;}}
  8. nodo*fine=new nodo(4,0);
  9. nodo*mezzo=new nodo(7,fine);
  10. nodo*inizio=new nodo(19,mezzo);
  11.  
  12.  
  13. //stampare una lista
  14. void stampalista(nodo*x){
  15. if(x){
  16. cout<<x->info;
  17. stampalista(x->next);
  18. }
  19. }
  20.  
  21. //inserire un nodo alla fine di una lista
  22. nodo*inseriscifine(nodo*x,int numero){
  23. if(!x) return new nodo(numero,0);
  24. else{
  25. x->next=inseriscifine(x->next,numero);
  26. return x;
  27. }
  28. }
  29. //inserire un nodo alla fine di una lista con passaggio per riferimento
  30. nodo*inserisciref(nodo*&x,int numero){
  31. if(!n) x=new nodo(numero,0);
  32. else inserisciref(n->next, numero);
  33. }
  34.  
  35. //inserire nodi in maniera ordinata in una lista
  36.  
  37. nodo* Inserisciord(nodo* n, int x)
  38. {
  39. if(!n)
  40. return new nodo(x,0);
  41. if(n->info>x)
  42. {
  43. nodo* copia=new nodo(x,n);
  44. return copia;
  45. }
  46. else
  47. n->next=Inserisciord(n->next,x);
  48. return n;
  49. }
  50.  
  51. nodo* F(nodo* n)
  52. {
  53. int x;
  54. cin>>x;
  55. if(x==-1)
  56. return n;
  57. else
  58. n=Inserisciord(n,x);
  59. return F(n);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement