Advertisement
Nello96

Gestore liste 0.2.3

May 6th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct nodo *lista;
  5.  
  6. typedef struct nodo {
  7.     int val;
  8.     lista next;
  9. } nodo;
  10.  
  11. lista creaLista (void);
  12. lista inserTesta (lista L, int elem);
  13. void inserCoda (lista L, int elem);
  14. lista ultimoNodo (lista L);
  15. void stampaLista (lista L);
  16. lista ricercaNodo (lista L, int key);
  17. void inserMezzo (lista L, int elem, int key);
  18. int lunghezzaLista (lista L);
  19. void inserisciInPosizione (lista L, int elem, int pos);
  20. void cancellaLista(lista L);
  21. void cancTesta (lista *L);
  22. void cancCoda (lista L);
  23. lista invertiLista (lista L);
  24.  
  25. int main (void) {
  26.     lista L, F;
  27.     L = creaLista();
  28.     stampaLista(L);
  29.     stampaLista(L);
  30. }
  31.  
  32. lista creaLista (void) {
  33.     lista L = NULL, past = NULL;
  34.     nodo N;
  35.     int buffer, primo = 1, ris, key, pos;
  36.     while (1) {
  37.         printf("\n--------------------------------------------------\nInserisci il prossimo valore (0 per terminare): ");
  38.         scanf("%d", &buffer);
  39.         if (buffer == 0) {
  40.             break;
  41.         }
  42.         else {
  43.             if (primo == 1) {
  44.                 past = L;
  45.                 L = inserTesta (L, buffer);
  46.                 if (past == L) {
  47.                     printf("Inserimento in testa fallito\n");
  48.                     return L;
  49.                 }
  50.                 primo = 0;
  51.                 printf("--------------------------------------------------\n\n");
  52.             }
  53.             else {
  54.                 printf("\nPosizioni d'inserimento disponibili:\n1. In testa\n2. In coda\n3. All'interno\n4. In una posizione specificata\n\nDove vuoi inserire l'elemento? ");
  55.                 scanf("%d", &ris);
  56.                 if (ris == 1) {
  57.                     L = inserTesta (L, buffer);
  58.                 }
  59.                 else if (ris == 2) {
  60.                     inserCoda (L, buffer);
  61.                 }
  62.                 else if (ris == 3) {
  63.                     printf("\nDopo quale elemento vuoi inserire? ");
  64.                     scanf("%d", &key);
  65.                     inserMezzo(L, buffer, key);
  66.                 }
  67.                 else if (ris == 4) {
  68.                     printf("In che posizione vuoi inserire l'elemento? ");
  69.                     scanf("%d", &pos);
  70.                     inserisciInPosizione(L, buffer, pos);
  71.                 }
  72.                 printf("--------------------------------------------------\n\n");
  73.             }
  74.         }
  75.     }
  76.     printf("--------------------------------------------------\n\n");
  77.     return L;
  78. }
  79.  
  80. lista inserTesta (lista L, int elem) {
  81.     nodo *N;
  82.     N = malloc(sizeof(nodo));
  83.     if (N == NULL) {
  84.         printf("Memoria insufficiente\n");
  85.     }
  86.     else {
  87.         N->next = L;
  88.         N->val = elem;
  89.         L = N;
  90.     }
  91.     return L;
  92. }
  93.  
  94. void inserCoda (lista L, int elem) {
  95.     nodo *N;
  96.     nodo *M;
  97.     N = malloc(sizeof(nodo));
  98.     if (N == NULL) {
  99.         return;
  100.     }
  101.     else {
  102.         M = ultimoNodo (L);
  103.         M->next = N;
  104.         N->val = elem;
  105.         N->next = NULL;
  106.     }
  107. }
  108.  
  109. lista ultimoNodo (lista L) {
  110.     while (L->next != NULL) {
  111.         L = L->next;
  112.     }
  113.     return L;
  114. }
  115.  
  116. lista ricercaNodo (lista L, int key) {
  117.     while ((L != NULL) && (L->val != key)) {
  118.         L = L->next;
  119.     }
  120.     if (L == NULL) {
  121.         printf("Valore non trovato\n");
  122.     }
  123.     return L;
  124. }
  125.  
  126. void inserMezzo (lista L, int elem, int key) {
  127.     nodo *F, *N;
  128.     F = ricercaNodo(L, key);
  129.     if (F == NULL) {
  130.         return;
  131.     }
  132.     else {
  133.         N = malloc(sizeof(nodo));
  134.         if (N == NULL) {
  135.             return;
  136.         }
  137.         else {
  138.             N->next = F->next;
  139.             F->next = N;
  140.             N->val = elem;
  141.         }
  142.     }
  143. }
  144.  
  145. int lunghezzaLista (lista L) {
  146.     int lunghezza = 1;
  147.     if (L == NULL) {
  148.         return 0;
  149.     }
  150.     else {
  151.         while (L->next != NULL) {
  152.             L = L->next;
  153.             lunghezza++;
  154.         }
  155.         return lunghezza;
  156.     }
  157. }
  158.  
  159. void inserisciInPosizione (lista L, int elem, int pos) {
  160.     int i;
  161.     nodo *M;
  162.     if (pos == 1) {
  163.         printf("Per inserire in testa, usa l'opzione 1\n");
  164.         return;
  165.     }
  166.     else if ((pos < 1) || (pos > lunghezzaLista(L))) {
  167.         printf("Valore non ammesso\n");
  168.         return;
  169.     }
  170.     else {
  171.         for (i=2; i<pos; i++) {
  172.             L = L->next;
  173.         }
  174.         M = malloc(sizeof(nodo));
  175.         if (M == NULL) {
  176.             printf("Memoria insufficiente\n");
  177.             return;
  178.         }
  179.         else {
  180.             M->next = L->next;
  181.             L->next = M;
  182.             M->val = elem;
  183.         }
  184.     }
  185. }
  186.  
  187. void cancellaLista (lista L) {
  188.    
  189. }
  190.  
  191. void cancTesta (lista *L) {
  192.     nodo *N;
  193.     if ((*L) != NULL) {
  194.         N = (*L);
  195.         (*L) = (*L)->next;
  196.         free(N);
  197.     }
  198.     else
  199.         return;
  200. }
  201.  
  202. void cancCoda (lista L) {
  203.     nodo *penultimo = L;
  204.     nodo *M;
  205.     M = ultimoNodo(L);
  206.     while ((penultimo->next)->next != NULL) {
  207.         penultimo = penultimo->next;
  208.     }
  209.     penultimo->next = NULL;
  210.     free(M);
  211. }
  212.  
  213. lista invertiLista (lista L) {
  214.     lista L1 = NULL;
  215.     nodo *succ;
  216.     while (L != NULL) {
  217.         succ = L->next;
  218.         L->next = L1;
  219.         L1 = L;
  220.         L = succ;
  221.     }
  222.     return L1;
  223. }
  224.  
  225. void stampaLista (lista L) {
  226.     nodo *N;
  227.     N = L;
  228.     printf("\n--------------------------------------------------\nI valori inseriti sono:\n\n");
  229.     while (N != NULL) {
  230.         printf("%d ", N->val);
  231.         N = N->next;
  232.     }
  233.     printf("\n--------------------------------------------------\n\n");
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement