如何使用malloc在链表中插入节点?
我正在编写一个程序,它在main中调用一个函数,创建一个节点来构建一个链表。程序从要放置在节点中的文件中读取字符。程序运行正常,直到进入创建节点的函数。我不确定是否存在逻辑或语法错误或其他错误。抱歉,长度太长了,但错误离开始不远。另外,如果需要我的头文件,请告诉我。代码来自我的C语言书:计算机科学:使用C语言的结构化编程方法,第三版。任何帮助和解释我的错误都将不胜感激 谢谢 这是主要的如何使用malloc在链表中插入节点?,c,linked-list,malloc,nodes,C,Linked List,Malloc,Nodes,我正在编写一个程序,它在main中调用一个函数,创建一个节点来构建一个链表。程序从要放置在节点中的文件中读取字符。程序运行正常,直到进入创建节点的函数。我不确定是否存在逻辑或语法错误或其他错误。抱歉,长度太长了,但错误离开始不远。另外,如果需要我的头文件,请告诉我。代码来自我的C语言书:计算机科学:使用C语言的结构化编程方法,第三版。任何帮助和解释我的错误都将不胜感激 谢谢 这是主要的 #include "my.h" int main (int argc, char* argv[]) {
#include "my.h"
int main (int argc, char* argv[])
{
int y;
NODE* pList;
NODE* pPre;
NODE* pNew;
DATA item;
FILE* fpntr;
int closeResult;
pList = NULL;
fpntr = fopen(argv[1], "r"); // open file
if(!fpntr)
{
printf("Could not open input file.\n");
exit (101);
}
else printf("The file opened.\n");
printf("starting InNode.\n"); //testing to see if this is working
while((y = fscanf(fpntr, "%c", &(item.key))) != EOF)
pList = InNode(pList, pPre, item);
printf("end InNode.\n"); //testing to see if this is working, doesn't get this far
printf("starting printme.\n");
printme(pList);
printf("end printme.\n");
closeResult = fclose(fpntr); //close file
if(closeResult == EOF)
{
printf("Could not close input file.\n");
exit (102);
}
else printf("The file closed.\n");
free(a);
return 0;
}
这是我书中的InNode.c:
#include "my.h"
NODE* InNode (NODE* pList, NODE* pPre, DATA item)
{
NODE* pNew;
printf("start malloc.\n"); //testing to see if this is working
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
printf("Memory overflow in insert.\n");
exit(100);
printf("malloc complete.\n"); //testing to see if this is working, doesn't get this far
pNew->data = item;
printf("start if statement.\n");
if (pPre == NULL)
{
pNew->link = pList;
pList = pNew;
}
else
{
pNew->link = pPre->link;
pPre->link = pNew;
}
printf("end else statement.\n");
return pList;
}
你的名字不见了{}
你的名字不见了{}
我立即注意到一个问题:
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
printf("Memory overflow in insert.\n");
exit(100);
您有一个if语句,主体周围没有大括号,两行缩进,好像它们应该在if语句主体内。只有第一行被解析为if语句的一部分;第二行exit100是无条件发生的,因此无论发生什么,您的程序都将退出
您可能应该将其更改为:
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
{
printf("Memory overflow in insert.\n");
exit(100);
}
如果这不能解决您的问题,或者一般来说,对于未来的问题,我建议您发布您得到的输出。如果您发布关于实际发生的情况的详细信息,例如输出、意外行为和您期望的结果等,而不是仅仅说没有更多细节就无法工作,人们会更容易发现问题。我立即注意到一个问题:
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
printf("Memory overflow in insert.\n");
exit(100);
您有一个if语句,主体周围没有大括号,两行缩进,好像它们应该在if语句主体内。只有第一行被解析为if语句的一部分;第二行exit100是无条件发生的,因此无论发生什么,您的程序都将退出
您可能应该将其更改为:
if (!(pNew = (NODE*) malloc(sizeof(NODE))))
{
printf("Memory overflow in insert.\n");
exit(100);
}
如果这不能解决您的问题,或者一般来说,对于未来的问题,我建议您发布您得到的输出。如果您发布关于实际发生情况的详细信息,例如输出、意外行为和您的预期等,人们会更容易发现问题,与其说它没有更多细节就不能工作。一个问题是在使用其值之前不初始化pPre一个问题是在使用其值之前不初始化pPre谢谢,这与初始化pPre一样有效。很抱歉遗漏了输出。下次我会记下来的@很高兴我能帮忙!是的,初始化pPre也很重要。我还注意到你在“它停止工作的地方”中添加了一条评论,但我没有看到这条评论,因为它太偏右了。所以,你确实发布了我要查找的信息,但格式可能更好。谢谢,这和初始化pPre一样有效。很抱歉遗漏了输出。下次我会记下来的@很高兴我能帮忙!是的,初始化pPre也很重要。我还注意到你在“它停止工作的地方”中添加了一条评论,但我没有看到这条评论,因为它太偏右了。所以,你确实发布了我想要的信息,但格式可能更好。