You have an endless loop because you do not scan for the next token in the loop body. You should write:
void tokeniseString(LinkedList *list, char *str) { char *token = strtok(str, " "); while (token != NULL) { insertLast(list, *token); token = strtok(NULL, " "); } }
Not however that you insert the value of the first byte of the token into the list. You should probably convert the token as a number using strtol() instead:
#include <errno.h> #include <limits.h> #include <string.h> #include <stdlib.h> void tokeniseString(LinkedList *list, char *str) { char *token = strtok(str, " \t\r\n"); while (token != NULL) { char *p; long value; errno = 0; value = strtol(token, &p, 10); if (p == token || *p != '\0') { fprintf(stderr, "token is not a number: %s\n", token); } else if (errno != 0 || value > INT_MAX || value < INT_MIN) { fprintf(stderr, "number is out of range: %s\n", token); } else { insertLast(list, (int)value); } token = strtok(NULL, " \t\r\n"); } }
Note that modifying the string argument is considered bad practice, especially using a function with side effects on a static state such as strtok(). Here is another version that does not modify the argument:
#include <errno.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <string.h> void tokeniseString(LinkedList *list, const char *str) { for (;;) { char *p; long value; int len; /* skip whitespace */ str += strspn(str, " \t\r\n"); if (*str == '\0') break; /* get the length of the token */ len = strcspn(str, " \t\r\n"); errno = 0; value = strtol(token, &p, 10); if (p == str) { fprintf(stderr, "token is not a number: %.*s\n", len, str); } else if (p != str + len) { fprintf(stderr, "token has extra characters: %.*s\n", len, str); } else if (errno != 0 || value > INT_MAX || value < INT_MIN) { fprintf(stderr, "number is out of range: %.*s\n", len, str); } else { insertLast(list, (int)value); } str += len; } }
Also note that you must close the file in readInputFile().
while (token != NULL) { insertLast (list, token); token = strtok (NULL, " "); }to insert all tokens in to list. Share a Minimal, Reproducible Example.