Advertisement
Guest User

esercizio 24

a guest
Dec 15th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 KB | None | 0 0
  1. main.c
  2.  
  3. #include "funz.h"
  4.  
  5. int main(){
  6.     char str[N] = "";
  7.     Lista *testa;
  8.     testa = NULL;
  9.     printf("\nInserire stringa(non più di 50 caratteri): ");
  10.     fgets(str, N, stdin);
  11.     testa = funzione(testa, str);
  12.     stampa(testa);
  13.     return 0;
  14. }
  15.  
  16. funz.h
  17.  
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <malloc.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #define N 51
  24.  
  25. typedef struct elemento{
  26.     char parola[N];
  27.     struct elemento *next;
  28. } Lista;
  29.  
  30.  
  31. Lista *funzione(Lista *, char []);
  32. Lista *inserimento(Lista *, char []);
  33. void stampa(Lista *);
  34.  
  35. funz.c
  36.  
  37. #include "funz.h"
  38.  
  39. Lista *funzione(Lista *h, char stringa[]){      //h è il puntatore alla testa della lista
  40.     int l, j = 0;
  41.     char strapp[N] = " ";           //stringa di appoggio dove inserisco la parola
  42.     l = strlen(stringa);
  43.     for(int i = 0; i<(l+1); i++){
  44.         if(stringa[i] != ' ' && stringa[i]!= '\n'){     //seconda condizione necessaria perchè utilizzo fgets
  45.             strapp[j] = stringa[i];            
  46.             j++;
  47.         }
  48.         if(stringa[i] == ' ' || stringa[i] == '\0'){
  49.             strapp[j] = '\0';                       //inserisco carattere di terminazione stringa
  50.             h = inserimento(h, strapp);             //inserisco la parola nella lista
  51.             strapp[0] = '\0';                       //"reinzializzo la stringa che contiene la parola"
  52.             j = 0;
  53.         }          
  54.     }
  55.     return h;
  56. }
  57.  
  58. Lista *inserimento(Lista *h, char string[N]){
  59.     Lista *app, *p;
  60.     p = h;
  61.     if(h == NULL){                      // se la lista è vuota inserisco la testa
  62.         h = (Lista *)malloc(sizeof(Lista));
  63.         if(h != NULL){
  64.             strcpy(h->parola, string);          //copio string(parola ricevuta in input) nel campo parola della lista
  65.             h->next = NULL;
  66.         } else printf("\nAlocazione non riuscita, valore non inserito in lista!");
  67.     }
  68.     else{           //se la lista non è vuota aggiungo in coda
  69.         app = (Lista *)malloc(sizeof(Lista));
  70.         if(app != NULL){
  71.             strcpy(app->parola, string);
  72.             app->next = NULL;
  73.             while(p->next != NULL) p = p->next;
  74.             p->next = app;
  75.         } else printf("\nAlocazione non riuscita, valore non inserito in lista!");
  76.     }
  77.     return h;
  78. }
  79.  
  80. void stampa(Lista *h){
  81.     Lista *p;
  82.     p = h;
  83.     printf("\n");
  84.     while(p != NULL){
  85.         printf("<%s>, ", p->parola);
  86.         p = p->next;
  87.     }
  88.     printf("\n");
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement