Advertisement
Guest User

Untitled

a guest
Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct lista{
  5. int dato;
  6. struct lista *prossimo;
  7. };
  8.  
  9. lista* crea(lista* testa);
  10. void stampa(lista* testa);
  11. lista* elimina(lista* testa);
  12. lista* inserire(lista* testa);
  13. int conta(lista* testa);
  14. lista* dealloca(lista* testa);
  15. lista* ordina(lista* testa);
  16. lista* casuale(lista* testa);
  17. void pari(lista* testa);
  18.  
  19. int main()
  20. {
  21. int choice;
  22. int menu = 1;
  23. int lungh;
  24. lista* head = NULL;
  25.  
  26. while(menu == 1)
  27. {
  28. printf("\n\n1.Crea lista\n2.Stampa lista\n3.Eliminare\n4.Inserire elemento\n5.Conta elementi\n6.Dealloca lista\n7.Ordina\n8.Numeri pari\n9.ESCI\n");
  29. scanf("%d", &choice);
  30.  
  31. switch (choice)
  32. {
  33. case 1:
  34. head = crea(head);
  35. break;
  36. case 2:
  37. stampa(head);
  38. break;
  39. case 3:
  40. head = elimina(head);
  41. break;
  42. case 4:
  43. head = inserire(head);
  44. break;
  45. case 5:
  46. lungh = conta(head);
  47. printf("\nSono presenti %d elementi", lungh);
  48. break;
  49. case 6:
  50. head = dealloca(head);
  51. break;
  52. case 7:
  53. head = ordina(head);
  54. break;
  55. case 8:
  56. pari(head);
  57. break;
  58. case 9:
  59. menu=0;
  60. break;
  61. default:
  62. printf("\nSelezionare numero esistente!");
  63. }
  64. }
  65.  
  66. return 0;
  67. }
  68.  
  69. lista* crea(lista* testa)
  70. {
  71. int n;
  72. int i;
  73. testa = (lista*)malloc(sizeof(lista));
  74. lista* nodo;
  75.  
  76. printf("\nInserire numero elementi: ");
  77. scanf("%d", &n);
  78.  
  79. printf("\nInserire elemento: ");
  80. scanf("%d", &testa->dato);
  81.  
  82. nodo = testa;
  83.  
  84. for(i=0; i<n-1; i++)
  85. {
  86. nodo->prossimo = (lista*)malloc(sizeof(lista));
  87. nodo = nodo->prossimo;
  88. printf("\nInserire elemento: ");
  89. scanf("%d", &nodo->dato);
  90. }
  91.  
  92. nodo->prossimo = NULL;
  93. return testa;
  94. }
  95.  
  96. void stampa (lista* testa)
  97. {
  98. lista* nodo;
  99. nodo = testa;
  100. if(nodo!=NULL)
  101. {
  102. printf("->%d", nodo->dato);
  103. stampa(nodo->prossimo);
  104. }
  105. }
  106.  
  107. lista* inserire(lista* testa)
  108. {
  109. int pos;
  110. int n;
  111. int i;
  112.  
  113. printf("\nSelezionare posizione del nuovo elemento: ");
  114. scanf("%d", &pos);
  115.  
  116. printf("\nInserire valore elemento: ");
  117. scanf("%d", &n);
  118.  
  119. lista* corr =(lista*)malloc(sizeof(lista));
  120. lista* succ = testa;
  121.  
  122. corr->dato = n;
  123.  
  124. if(pos==1)
  125. {
  126. corr->prossimo = testa;
  127. testa = corr;
  128. return testa;
  129. }
  130.  
  131. for(i=0; i<pos-2; i++)
  132. {
  133. succ = succ->prossimo;
  134. }
  135.  
  136. corr->prossimo = succ->prossimo;
  137. succ->prossimo = corr;
  138.  
  139. return testa;
  140. }
  141.  
  142. lista* elimina(lista* testa)
  143. {
  144. int del;
  145. int i;
  146. lista* corr;
  147. lista* succ;
  148.  
  149. corr = testa;
  150. succ = testa->prossimo;
  151.  
  152. printf("\nSelezionare numero posizione da elminare: ");
  153. scanf("%d", &del);
  154.  
  155. if(del==1)
  156. {
  157. testa = corr->prossimo;
  158. free(corr);
  159. return testa;
  160. }
  161. else
  162. {
  163. for(i=0; i<del-2; i++)
  164. {
  165. corr=corr->prossimo;
  166. printf("\nposizione %d", corr->prossimo);
  167. }
  168.  
  169. succ = corr->prossimo;
  170. corr->prossimo = succ->prossimo;
  171. free(succ);
  172. return testa;
  173. }
  174. }
  175.  
  176. int conta(lista* testa)
  177. {
  178. lista* nodo;
  179. nodo = testa;
  180.  
  181. if(nodo==NULL)
  182. {
  183. return 0;
  184. }
  185. else
  186. {
  187. return 1+conta(nodo->prossimo);
  188. }
  189. }
  190.  
  191. lista* dealloca(lista* testa)
  192. {
  193. lista* nodo;
  194. lista* succ;
  195. nodo = testa;
  196. if(nodo!=NULL)
  197. {
  198. succ = nodo->prossimo;
  199. free(nodo);
  200. nodo = succ;
  201. }
  202. return NULL;
  203. }
  204.  
  205. lista* ordina(lista* testa)
  206. {
  207. lista* curr;
  208. lista* succ;
  209. int tmp;
  210.  
  211. for(curr=testa; curr!=NULL; curr=curr->prossimo)
  212. {
  213. for(succ=curr->prossimo; succ!=NULL; succ=succ->prossimo)
  214. {
  215. if(curr->dato > succ->dato)
  216. {
  217. tmp = succ->dato;
  218. succ->dato = curr->dato;
  219. curr->dato = tmp;
  220. }
  221. }
  222. }
  223. return testa;
  224. }
  225.  
  226. void pari(lista* testa)
  227. {
  228. lista* nodo;
  229. nodo = testa;
  230.  
  231. if(nodo!=NULL)
  232. {
  233. if(nodo->dato%2 == 0)
  234. {
  235. printf("%d->", nodo->dato);
  236. pari(nodo->prossimo);
  237. }
  238. else
  239. {
  240. pari(nodo->prossimo);
  241. }
  242. }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement