Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node
  6. {
  7.     char word[100];
  8.     struct node *next;
  9. };
  10.  
  11. struct node *funct(char frase[]);
  12. void print_list(struct node *head);
  13. void append(struct node **head, int inizio, int fine, char frase[]);
  14.  
  15. int main()
  16. {
  17.     char frase[]="Esame di Calcolatori Elettronici";
  18.  
  19.     struct node *head=NULL;
  20.  
  21.     head=funct(frase);
  22.  
  23.     print_list(head);
  24.  
  25.     return 0;
  26. }
  27.  
  28. struct node *funct(char frase[])
  29. {
  30.     struct node *head=NULL;
  31.     int inizio=0,fine=0;
  32.  
  33.     for (int i = 0; i < (int)strlen(frase); i++)
  34.     {
  35.         if (frase[i]==' '||frase[i]=='\n'||frase[i]=='\t')
  36.         {
  37.             fine=i-1;
  38.             append(&head,inizio,fine,frase);
  39.             inizio=i+1;
  40.         }
  41.     }
  42.     append(&head,inizio,(int)strlen(frase),frase);
  43.     return head;
  44. }
  45.  
  46. void append(struct node **head, int inizio, int fine, char frase[])
  47. {
  48.     struct node *new;
  49.     new=malloc(sizeof(struct node));
  50.     if (new==NULL)
  51.     {
  52.         printf("malloc failed\n");
  53.         exit(EXIT_FAILURE);
  54.     }
  55.     int j=0;
  56.     for (int i = inizio; i <=fine; i++)
  57.     {
  58.         new->word[j]=frase[i];
  59.         j++;
  60.     }
  61.     new->word[j+1]='\0';
  62.  
  63.     new->next=NULL;
  64.    
  65.     if (*head==NULL)
  66.     {
  67.         *head=new;
  68.     }
  69.     else
  70.     {
  71.         struct node *current;
  72.         current=*head;
  73.         while(current->next!=NULL)
  74.         {
  75.             current=current->next;
  76.         }
  77.         current->next=new;
  78.     }
  79.     return;
  80. }
  81.  
  82.  
  83. void print_list(struct node *head)
  84. {
  85.     struct node *current;
  86.     current=head;
  87.  
  88.     if(current==NULL)
  89.     {
  90.         printf("La lista e' vuota!\n");
  91.         return;
  92.     }
  93.     printf("La lista e':\n");
  94.     for (current=head;current!=NULL; current=current->next)
  95.     {
  96.         printf("[%s]-->",current->word);
  97.     }
  98.     printf("NULL\n");
  99.  
  100.     return;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement