我有这段代码,但我不知道为什么这不起作用?inputData() 函数似乎可以工作,但 print 方法只打印我发送给 inputDate() 的第一行,因为我输入了尽可能多的行。
我正在从文件中读取,一次一行,然后放入链接列表,这就是问题所在。如果我在代码中传递值,那么没有问题吗?
//LINKED LIST
void inputData(char *l)
{
struct lines *pNewStruct = (struct lines *) malloc(sizeof(struct lines));
pNewStruct->line = l;
//inserts if list empty, next set to null
if(pFirstNode == NULL){
pNewStruct->next = NULL;
pFirstNode = pLastNode = pNewStruct;
} else {
//inserts if list contains one element
//this is done to differentiate between first and last node
if(pFirstNode == pLastNode) {
pFirstNode->next = pNewStruct;
pLastNode = pNewStruct;
pNewStruct->next = NULL;
//inserts elements when elements in list > 2
} else {
pLastNode->next = pNewStruct;
pNewStruct->next = NULL;
pLastNode = pNewStruct;
}
}
}
void printData()
{
struct lines *temp = pFirstNode;
while(temp != NULL)
{
printf("%s", temp->line);
temp = temp->next;
}
}