Я пытаюсь написать функцию words
, которая создает односвязный список слов (последовательности символов, разделенных пробелами) из текста, передаваемого в качестве параметра.Слова в результирующем списке должны быть такими же, как и в тексте.
К сожалению, программа выдает ошибку во время работы, не могли бы вы объяснить, что идет не так, и я также был бы признателен за некоторые советы.Вот код:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
struct node{
char* word;
struct node* next;
};
void printList(struct node* list){
struct node* it = list;
while(it != NULL){
printf("%s ", it -> word);
it = it -> next;
}
printf("\n");
}
void insertLast(struct node* tail, char* neww){
tail -> next = (struct node*)malloc(sizeof(struct node));
tail = tail -> next;
tail -> word = neww;
tail -> next = NULL;
}
struct node* words(char* s){
char* slowo = strtok(s, " ");
struct node* head;
struct node* tail;
if (sizeof(slowo) == 0)
return NULL ;
head = (struct node*)malloc(sizeof(struct node));
head -> word = slowo;
head -> next = NULL;
tail = head;
slowo = strtok(NULL, " ");
while (slowo != NULL){
insertLast(tail, slowo);
tail = tail -> next;
slowo = strtok(NULL, " ");
}
return head;
}
int main() {
printList(words("Some sentance la al olaalal"));
getch();
return (EXIT_SUCCESS);
}