Guest User

Untitled

a guest
Jun 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. //============================================================================
  2. // Name : List_Nopari_Giusè.cpp
  3. // Author :
  4. // Version :
  5. // Copyright : Your copyright notice
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10. using namespace std;
  11.  
  12. struct nodo {
  13. int val;
  14. nodo* next;
  15. };
  16.  
  17. nodo* ins_in_testa(nodo* root)
  18. {
  19. int n;
  20. nodo* tmp=root;
  21. cout<<"Dammi il valore da inserire: \n";
  22. cin>>n;
  23. root=new nodo;
  24. root->val=n;
  25. root->next=tmp;
  26. return(root);
  27. }
  28.  
  29. nodo* ins_in_coda(nodo* root)
  30. {
  31. nodo* tmp=root;
  32. nodo* nuovonodo;
  33. int n;
  34. cout<<"Dammi il valore da inserire: \n";
  35. cin>>n;
  36. nuovonodo=new nodo;
  37. nuovonodo->val=n;
  38. nuovonodo->next=NULL;
  39. if (root==NULL)
  40. root=nuovonodo;
  41. else
  42. {
  43. while(tmp->next!=NULL) tmp=tmp->next;
  44. tmp->next=nuovonodo;
  45. }
  46. return(root);
  47. }
  48.  
  49. nodo* ins_ord_c (nodo* root, int a)
  50. {
  51. if (root==NULL) { //caso lista vuota
  52. root=new nodo;
  53. root->val=a;
  54. root->next=NULL;
  55. }
  56. else { //ci sono altri elementi
  57. if (a<(root->val)) {//da inserire in cima alla lista
  58. nodo* tmp=root;
  59. root=new nodo;
  60. root->val=a;
  61. root->next=tmp;
  62. }
  63. else { //caso generale
  64. nodo*tmp1=root;
  65. nodo*tmp2=root;
  66. while ( a >(tmp1->val) ) tmp1=tmp1->next; //quella successiva alla nostra
  67. while ((tmp2->next)!=tmp1) tmp2=tmp2->next; //tmp2 punta la precendente
  68. tmp2->next=new nodo;
  69. tmp2->next->val=a;
  70. tmp2->next->next=tmp1;
  71. }
  72. }
  73. return root;
  74. };
  75.  
  76. nodo* crea_lista(nodo* root)
  77. {
  78. char a;
  79. do
  80. {
  81. root=ins_in_coda(root);
  82. cout<<"Vuoi inserire un nuovo elemento?(s/n)";
  83. cin>>a;
  84. }
  85. while(a=='s');
  86. return(root);
  87. }
  88.  
  89. nodo* crea_lista_ord(nodo* root)
  90. {
  91. char a;
  92. int b;
  93. do
  94. {
  95. cout<<"Dammi il valore: \n";
  96. cin>>b;
  97. root=ins_ord_c(root,b);
  98. cout<<"Vuoi inserire un nuovo elemento?(s/n)";
  99. cin>>a;
  100. }
  101. while(a=='s');
  102. return(root);
  103. }
  104.  
  105. void stampa(nodo* root)
  106. {
  107. while(root!=NULL)
  108. {
  109. cout<<root->val<<" ";
  110. root=root->next;
  111. }
  112. cout<<"\n";
  113. }
  114.  
  115. nodo* rim_pari(nodo* root)
  116. {
  117. nodo* curr=root;
  118. if (root==NULL) cout<<"Lista Vuota\n"; //Messaggio in caso lista vuota
  119. while (curr!=NULL)
  120. {
  121. if ( (curr->val)%2!=0 ) {(curr->val)=(curr->val)*3; curr=curr->next;} //Caso Dispari
  122. else
  123. {
  124. if (curr==root) //Se curr è uguale a root, allora è il primo nodo della lista
  125. {
  126. curr=curr->next;
  127. delete root;
  128. root=curr;
  129. }
  130. else
  131. {
  132. nodo* tmp1=root;
  133. nodo* tmp2=curr;
  134. while ((tmp1->next)!=curr) tmp1=tmp1->next;
  135. curr=curr->next;
  136. delete tmp2;
  137. tmp1->next=curr;
  138. }
  139.  
  140. }
  141. }
  142. return (root);
  143. }
  144.  
  145. int main()
  146. {
  147. nodo* root=NULL;
  148. root=crea_lista_ord(root);
  149. stampa(root);
  150. /*nodo *puntlist = NULL;
  151. puntlist = crea_lista (puntlist);
  152. stampa (puntlist);
  153. puntlist=rim_pari(puntlist);
  154. stampa(puntlist);*/
  155. return 0;
  156. }
Add Comment
Please, Sign In to add comment