I am having trouble with a programming assignment in which I have to read a file, create a singly linked list, delete the nodes from the list, and print the modified list.
My biggest problem so far is writing code that builds the list so far I have.
The first problem is that, when I go to read the file, the program spontaneously quits. However, when I remove fscanf(spListData, "%d%*c ", ptNew -> listEl); from the code the program still spontaneously quit. As far as I can figure out the first part of the if-else statement, which insert a node into an empty list, but the program either segfault or loops infinitely, depending on condition in the while loop.Code:void bldList (FILE* spListData, struct node* ptLnkList)
{
struct node* ptNew;
struct node* ptCur;
while (!feof(spListData))
{
ptNew = (struct node*) malloc (sizeof(struct node));
if (ptNew == NULL)
{
printf("Error allocating node.");
exit(201);
}
fscanf(spListData, "%d%*c ", ptNew -> listEl);
if (ptLnkList == NULL)
{
ptLnkList = ptNew;
ptNew -> link = NULL;
}
else
{
ptCur = ptLnkList;
while (ptCur != NULL)
{
ptCur = ptCur -> link;
}
ptCur -> link = ptNew;
ptNew -> link = NULL;
}
}
return;
}
If the condition is ptCur != NULL, it segfaults; if the condition is ptCur -> link != NULL, it loops infinitely. The former makes sense since there are references in the code to ptCur -> link after ptCur -> link has been assigned NULL. The latter, however, make little sense to me because the behavior of the program is governed by the outer while loop, which is dependent on feof() returning 0, which it apparently must do at some point if the file contains an EOF character.
Any ideas?