Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node
- {
- char word[100];
- struct node *next;
- };
- struct node *funct(char frase[]);
- void print_list(struct node *head);
- void append(struct node **head, int inizio, int fine, char frase[]);
- int main()
- {
- char frase[]="Esame di Calcolatori Elettronici";
- struct node *head=NULL;
- head=funct(frase);
- print_list(head);
- return 0;
- }
- struct node *funct(char frase[])
- {
- struct node *head=NULL;
- int inizio=0,fine=0;
- for (int i = 0; i < (int)strlen(frase); i++)
- {
- if (frase[i]==' '||frase[i]=='\n'||frase[i]=='\t')
- {
- fine=i-1;
- append(&head,inizio,fine,frase);
- inizio=i+1;
- }
- }
- append(&head,inizio,(int)strlen(frase),frase);
- return head;
- }
- void append(struct node **head, int inizio, int fine, char frase[])
- {
- struct node *new;
- new=malloc(sizeof(struct node));
- if (new==NULL)
- {
- printf("malloc failed\n");
- exit(EXIT_FAILURE);
- }
- int j=0;
- for (int i = inizio; i <=fine; i++)
- {
- new->word[j]=frase[i];
- j++;
- }
- new->word[j+1]='\0';
- new->next=NULL;
- if (*head==NULL)
- {
- *head=new;
- }
- else
- {
- struct node *current;
- current=*head;
- while(current->next!=NULL)
- {
- current=current->next;
- }
- current->next=new;
- }
- return;
- }
- void print_list(struct node *head)
- {
- struct node *current;
- current=head;
- if(current==NULL)
- {
- printf("La lista e' vuota!\n");
- return;
- }
- printf("La lista e':\n");
- for (current=head;current!=NULL; current=current->next)
- {
- printf("[%s]-->",current->word);
- }
- printf("NULL\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement