Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. struct nodo{
  5. int info;
  6. struct nodo *next;
  7. };
  8. typedef struct nodo Nodo;
  9. Nodo* inserisciNodoInTesta(Nodo* first, int value);
  10. void stampaLista(Nodo* first);
  11. void stampaListaRicorsiva(Nodo* first);
  12. void eliminaLista(Nodo* first);
  13. void eliminaListaRicorsiva(Nodo* first);
  14. Nodo* inserisciNodoInCoda(Nodo* first);
  15. int main()
  16. {
  17. Nodo* lista=NULL;
  18. int n,i,num;
  19. srand(time(NULL));
  20. printf("Inserisci il numero di elementi\n");
  21. scanf("%d",&n);
  22. for(i=0;i<n;i++){
  23. printf("Inserisci valore\n");
  24. scanf("%d",&num);
  25. lista=inserisciNodoInTesta(lista, num);
  26. }
  27. stampaLista(lista);
  28. printf("\n");
  29. stampaListaRicorsiva(lista);
  30. printf("\n");
  31. lista=inserisciNodoInCoda(lista);
  32. stampaLista(lista);
  33. return 0;
  34. }
  35. Nodo* inserisciNodoInTesta(Nodo* first, int value){
  36. Nodo* tmp=NULL;
  37. tmp=(Nodo*)malloc(sizeof(Nodo));
  38. if(tmp==NULL){
  39. exit(-1);
  40. }
  41. tmp->info=value;
  42. tmp->next=first;
  43. return tmp;
  44. }
  45. void stampaLista(Nodo* first){
  46. while(first!=NULL){
  47. printf("%d ",first->info);
  48. first=first->next;
  49. }
  50. }
  51. void stampaListaRicorsiva(Nodo* first){
  52. if(first!=NULL){
  53. printf("%d ",first->info);
  54. stampaListaRicorsiva(first->next);
  55. }
  56. }
  57. void eliminaLista(Nodo* first){
  58. Nodo* tmp;
  59. while(first!=NULL){
  60. tmp=first->next;
  61. free(first);
  62. first=tmp;
  63. }
  64. }
  65. void eliminaListaRicorsiva(Nodo* first){
  66. if (first!=NULL){
  67. free(first);
  68. eliminaListaRicorsiva(first->next);
  69. }
  70. }
  71. Nodo* inserisciNodoInCoda(Nodo* first){
  72. Nodo* tmp;
  73. tmp=(Nodo*)malloc(sizeof(Nodo));
  74. if(tmp==NULL){
  75. exit(-1);
  76. }
  77. tmp->info=3;
  78. tmp->next=NULL;
  79.  
  80. while(first->next!=NULL){
  81.  
  82. first=first->next;
  83.  
  84. }
  85. first->next=tmp;
  86.  
  87. return first;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement