Advertisement
Guest User

Untitled

a guest
May 4th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct nodo *lista;
  6.  
  7. typedef struct nodo {
  8. int val;
  9. lista next;
  10. }nodo;
  11.  
  12. lista listaVuota (void);
  13. lista inserTesta(lista L, int el);
  14. lista cancTesta (lista L);
  15. lista inserCoda(lista L, int el);
  16. void stampaLista(lista L);
  17. lista leggiLista(void);
  18.  
  19. int main(void)
  20. {
  21. lista L;
  22. L= leggiLista();
  23. stampaLista(L);
  24. return 0;
  25. }
  26.  
  27. lista listaVuota(void)
  28. {
  29. return NULL;
  30. }
  31. void stampaLista (lista L)
  32. {
  33. printf("\nLa lista contiene gli elementi:\n\n");
  34. while(L!=NULL)
  35. {
  36. printf("%d\n",L->val);
  37. L= L->next;
  38. }
  39. printf("\n\n");
  40. }
  41.  
  42. lista cancTesta(lista L)
  43. {
  44. lista temp;
  45. if(L==listaVuota()) return listaVuota();
  46. temp = L;
  47. L = L-> next;
  48. free(temp);
  49. return L;
  50. }
  51.  
  52. lista inserTesta(lista L,int el)
  53. {
  54. nodo *N;
  55. N = malloc (sizeof(nodo));
  56. if(N == NULL)
  57. return L;
  58. N->val = el;
  59. N->next = L;
  60. return N;
  61. }
  62.  
  63. lista leggiLista (void)
  64. {
  65. lista L = listaVuota(),prec = L;
  66. int val;
  67. while (1)
  68. {
  69. printf("\nInserire prossimo valore (0 per terminare):");
  70. scanf("%d", &val);
  71. if(val == 0) break;
  72. temp = inserCoda(L, val);
  73. if (temp == NULL)
  74. {
  75. printf("\n\nMemoria esaurita, inserimento terminato\n\n");
  76. break;
  77. }
  78. L = temp;
  79. }
  80. return L;
  81. }
  82.  
  83. lista inserCoda(lista L, int el)
  84. {
  85. nodo *N, *temp;
  86. temp = L;
  87. if(L == NULL)
  88. return inserTesta(L,el);
  89. while (tmp->next != NULL)
  90. temp= temp->next;
  91. N=malloc (sizeof(nodo));
  92. if (N == NULL)
  93. return NULL;
  94. N->val = el;
  95. temp->next = N;
  96. N->next =NULL;
  97. return L;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement